C# Digital Ocean Spaces 发出请求时出错,错误代码为禁止,Http 状态代码为禁止。没有返回更多错误信息

问题描述 投票:0回答:1

我正在使用 Digital Ocean Spaces,但我无法使用 AWS.SDK 从我的 Dotnet Web Api 应用程序正确上传图像,我收到错误:

发出请求时出错,错误代码为禁止,Http 状态代码为禁止。没有返回更多错误信息。

这是我的代码,也尝试使用注释代码相同的错误:

        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;
            }
        }

请问你能帮我吗?另外,当上面的代码有效时,我不希望图像在默认添加时具有私有权限但具有公共权限?

我已经尝试了一切,查看了其他问题,例如:

https://www.digitalocean.com/community/questions/uploading-file-to-do-spaces-using-aws-sdk-net-net-core

我期待图片上传成功!

c# amazon-s3 digital-ocean digital-ocean-spaces
1个回答
0
投票

将 Amazon S3 SDK nuget 包降级到 3.7.102 解决了我的问题。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.