将文件上传到Amazon S3存储桶

时间:2018-05-03 13:25:10

标签: c# .net amazon-web-services amazon-s3

我知道有很多这样的线程,我尝试了100个例子,所有这些都抛出相同的异常:

  

指定的存储桶无效。

我联系了s3管理员,他说,一切都是正确的,他说,他设置了完整的权限,没有拒绝声明,所以我们可以测试, 但我的应用程序仍然抛出相同的异常,这是代码..

using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;

namespace AmazonS3
{
    public class FileUploader
    {
        private const string S3_Bucket_ARN = "(hidden)";
        private const string IAM_User_ARN = "(hidden)";
        private const string Access_Key_ID = "(hidden)";
        private const string Secret_access_Key = "(hidden)";

        public void UploadFile(string Filepath)
        {
            Program.WriteLine(MessageStatus.Neutral, "[1/4] Connecting to Amazon S3 . . .");
            using (IAmazonS3 client = new AmazonS3Client(Access_Key_ID, Secret_access_Key, RegionEndpoint.USEast1))
            {
                Program.WriteLine(MessageStatus.Neutral, "[2/4] Preparing upload request . . .");
                var uploadRequest = new TransferUtilityUploadRequest
                {
                    FilePath = Filepath,
                    BucketName = S3_Bucket_ARN,
                    CannedACL = S3CannedACL.PublicRead
                };

                Program.WriteLine(MessageStatus.Neutral, $"[3/4] Uploading file: \"{Filepath}\" . . .");
                var fileTransferUtility = new TransferUtility(client);
                fileTransferUtility.Upload(uploadRequest);

                Program.WriteLine(MessageStatus.Neutral, $"[4/4] File: \"{Filepath}\" successfully uploaded.");
            }
        }
    }
}

隐藏的字符串当然被我的真实凭据所取代,我现在只是编辑它们。 无论如何这里看起来不正确?我现在已经坚持好几天了,谢谢你。

2 个答案:

答案 0 :(得分:2)

我最近使用了以下方法可能有帮助

        private string UploadAWS(Stream stream, string contentType, string name,string orgfileName,string lable)
    {
        string accessKey = "xxxxx";
        string secretKey = "xxxxxxx";

        using (client = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast1))
        {
            return WritingAnObject(stream, contentType, name, orgfileName,lable);
        }
    }

    private string WritingAnObject(Stream stream, string contentType, string name,string orgfileName,string lable)
    {
        string rtn = string.Empty;

        try
        {
            string bucketName = "xxxxxxxx";

            PutObjectRequest putRequest2 = new PutObjectRequest
            {
                BucketName = bucketName,
                Key = name,
                ContentType = contentType,
                InputStream = stream
            };
            putRequest2.Metadata.Add("x-amz-meta-title", lable);
            putRequest2.Metadata.Add("x-amz-meta-original-file-name", orgfileName);

            PutObjectResponse response2 = client.PutObject(putRequest2);

        }
        catch (AmazonS3Exception amazonS3Exception)
        {
            if (amazonS3Exception.ErrorCode != null &&
                (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                ||
                amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
            {
                rtn="Check the provided AWS Credentials.";
            }
            else
            {
                rtn="Error occurred. Message:"+ amazonS3Exception.Message + " when writing an object";
            }
        }

        return rtn;

    }

答案 1 :(得分:1)

问题在于:

BucketName = S3_Bucket_ARN

ARN是Amazon Resource Names,它是用于唯一标识亚马逊资源的命名空间......但在这种情况下,BucketName只期望存储桶的实际名称,例如: my-example-bucket

如果您正在使用存储桶ARN,则传递的值看起来像arn:aws:s3:::my-example-bucket,这不仅不是存储桶名称,还包含不是“#{1}}的字符(:)。在桶名称中有效。

S3 API Reference表示当存储桶名称不正确时,可能存在两种不同的错误:

InvalidBucketName | The specified bucket is not valid.
NoSuchBucket      | The specified bucket does not exist.

第一个意味着存储桶名称包含无效字符。