Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upload image #87

Merged
merged 5 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Email", "FirstName", "ImageURL", "LastName", "Password", "PhoneNumber", "RoleId", "Username" },
values: new object[] { new Guid("eb35fe46-76b2-4809-bc1b-c3ac0b3357e4"), "[email protected]", "admin", null, "admin", "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=", "09131111111", 1, "admin" });
values: new object[] { new Guid("ac34d5b9-c1f0-4f90-bf35-6a80b25c96b0"), "[email protected]", "admin", null, "admin", "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=", "09131111111", 1, "admin" });

migrationBuilder.CreateIndex(
name: "IX_EntityEdges_EntityIDSource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasData(
new
{
Id = new Guid("eb35fe46-76b2-4809-bc1b-c3ac0b3357e4"),
Id = new Guid("ac34d5b9-c1f0-4f90-bf35-6a80b25c96b0"),
Email = "[email protected]",
FirstName = "admin",
LastName = "admin",
Expand Down
3 changes: 2 additions & 1 deletion AnalysisData/AnalysisData/User/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public async Task<IActionResult> GetUserInformation()
FirstName = result.FirstName,
LastName = result.LastName,
PhoneNumber = result.PhoneNumber,
Email = result.Email
Email = result.Email,
Image = result.ImageURL ?? "User do not have information yet !"
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public class S3FileStorageService : IS3FileStorageService

public S3FileStorageService(IConfiguration configuration)
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(configuration["AWS:AccessKey"], configuration["AWS:SecretKey"]);
var config = new AmazonS3Config
{
ServiceURL = configuration["AWS:ServiceURL"]
};
_s3Client = new AmazonS3Client(awsCredentials, config);
_bucketName = configuration["AWS:BucketName"];
_s3Client = new AmazonS3Client(
configuration["AWS:AccessKey"],
configuration["AWS:SecretKey"],
RegionEndpoint.GetBySystemName(configuration["AWS:Region"])
);
}

public async Task<string> UploadFileAsync(IFormFile file, string folderName)
Expand All @@ -32,8 +33,15 @@ public async Task<string> UploadFileAsync(IFormFile file, string folderName)
ContentType = file.ContentType
};

await _s3Client.PutObjectAsync(putRequest);

return $"https://{_bucketName}.s3.amazonaws.com/{fileKey}";
var response = await _s3Client.PutObjectAsync(putRequest);
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
var fileUrl = $"https://{_bucketName}.s3.{RegionEndpoint.USEast1.SystemName}.amazonaws.com/{fileKey}";
return fileUrl;
}
else
{
return "Could not upload .";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<bool> UploadImageAsync(ClaimsPrincipal claimsPrincipal, IFormF
string imageUrl = null;
if (file != null && file.Length > 0)
{
imageUrl = await _s3FileStorageService.UploadFileAsync(file, "UserImages");
imageUrl = await _s3FileStorageService.UploadFileAsync(file, "usersProfile");
user.ImageURL = imageUrl;
}
user.ImageURL = imageUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public class GetUserInformationDto
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }

public string Image { get; set; }
}
9 changes: 5 additions & 4 deletions AnalysisData/AnalysisData/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"UseS3": true
},
"AWS": {
"Region": "us-west-2",
"AccessKey": "956e557a5a204182972e38802cbb5eba",
"SecretKey": "c4bbe0898e3746e08d33ddcde7e6d55e",
"BucketName": "backend-abriment-com"
"Region": "us-east-1",
"AccessKey": "d022cd7a-8e0f-4e0d-b305-ae64116028c0",
"SecretKey": "19b8c45a60e797da10801b01c5a264804e3cafa07c77ec7bf4c40c09161523f2",
"BucketName": "stardata",
"ServiceURL": "https://s3.ir-thr-at1.arvanstorage.ir"
},
"AllowedHosts": "*"
}
Loading