使用传输实用程序

时间:2018-01-21 21:21:56

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

我已经尝试了几个小时才能让这个工作,并且不确定我做错了什么。我在俄勒冈州西部有一个水桶(us-west-2)。我有一个具有完全S3访问权限的IAM用户。我试图将字符串的内容存储在S3上的文件中。以下是代码:

using System.IO;
using Amazon;

namespace XXX.Util
{

    public static class S3
    {

        private static Amazon.S3.Transfer.TransferUtility transferUtility;

        public static void UploadFile(string bucket, string key, string contents)
        {    
            // user "publisher" credentials
            transferUtility = new Amazon.S3.Transfer.TransferUtility("{AccessID}", "{Secret}",RegionEndpoint.USWest2);

            using (Stream s = GenerateStreamFromString(contents))
            {
                using (transferUtility)
                {
                    transferUtility.Upload(s, bucket, key);
                }
            }


        }
        public static Stream GenerateStreamFromString(string s)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }
    }
}

我用以下方法调用方法:

S3.UploadFile("s3-us-west-2.amazonaws.com/{bucket-name}", guid.ToString(),contents);

我得到的错误信息是:

  

AmazonS3Exception:拒绝访问   Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException(IExecutionContext   executionContext,HttpErrorResponseException异常)   HttpErrorResponseExceptionHandler.cs,第60行

我的S3政策:

  

{       "版本":" 2012-10-17",       " Id":" Policy1516569218147",       "陈述":[           {               " Sid":" Stmt1516569211561",               "效果":"允许",               "校长":{                   " AWS":" arn:aws:iam :: {iamID}:user / publisher"               },               "行动":" s3:*",               "资源":" arn:aws:s3 ::: {bucket-name}"           }       ]}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据documentation

S3位置与格式s3:// bucket / path相匹配。不要包括 端点。例如,s3://us-east-1.amazonaws.com/bucket/path 会导致“访问被拒绝”错误。