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
{
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
// };
// 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 (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:
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.