Reputation: 11
I’m using Digital Ocean Spaces but I can’t upload images correctly from my Dotnet Web Api application using AWS.SDK I get error:
Error making request with Error Code Forbidden and Http Status Code Forbidden. No further error information was return.
This is my code, also tried with the commented code same error:
public async Task<bool> AddUserImage(ImageCreateDTO imageCreateDto)
{
try
{
var credentials = new BasicAWSCredentials(_storageOptions.Value.AccessKey, _storageOptions.Value.SecretKey);
AmazonS3Config config = new()
{
ServiceURL = _storageOptions.Value.ServiceUrl,
};
AmazonS3Client s3Client = new(
_storageOptions.Value.AccessKey,
_storageOptions.Value.SecretKey,
config
);
using var stream = imageCreateDto.File.OpenReadStream();
TransferUtility fileTransferUtility = new(s3Client);
TransferUtilityUploadRequest fileTransferUtilityRequest = new()
{
BucketName = $"joinorcreate/{_storageOptions.Value.UserImages}",
StorageClass = S3StorageClass.Standard,
PartSize = imageCreateDto.File.Length,
Key = imageCreateDto.FileName,
CannedACL = S3CannedACL.PublicRead,
InputStream = stream
};
// Add the x-amz-acl header
fileTransferUtilityRequest.Headers["x-amz-acl"] = "public-read";
await fileTransferUtility.UploadAsync(fileTransferUtilityRequest);
// PutObjectRequest request = new()
// {
// BucketName = $"joinorcreate/{_storageOptions.Value.UserImages}",
// Key = imageCreateDto.FileName,
// InputStream = stream,
// ContentType = imageCreateDto.File.ContentType,
// CannedACL = S3CannedACL.PublicRead,
// StorageClass = S3StorageClass.Standard,
// };
// await s3Client.PutObjectAsync(request);
return true;
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e.Message);
return false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
Please can you help me? Also I don’t want the images to have permission private but public when added by default, when the code above works?
I’ve tried everything, looked at other issues like:
I was expecting the image to be uploaded successfully!
Upvotes: 1
Views: 211
Reputation: 3474
Downgrading the Amazon S3 SDK nuget package to 3.7.102 fixed the issue for me.
Upvotes: 0