1

我正在尝试使用 .NET SDK 将文件上传到 Amazon S3。上传工作正常,除了它对上传的文件应用默认权限,即“私人”。

我希望将上传文件的权限设置为“BucketOwnerFullControl”。

下面是我的代码,我不太确定我需要做什么才能让它正常工作。

    static string accessKeyID = "";
    static string secretAccessKey = "";
    static string existingBucketName = "bucket";

    static void Main(string[] args)
    {
        NameValueCollection appConfig = ConfigurationManager.AppSettings;
        accessKeyID = appConfig["accesskey"];
        secretAccessKey = appConfig["secretkey"];

        string[] files = Directory.GetFiles("C:\\amazontestdirectory");

            foreach (string fileName in files)
            {
                try
                {
                    TransferUtility fileTransferUtility = new TransferUtility(accessKeyID, secretAccessKey);

                    // Upload a file, file name is used as the object key name.
                    fileTransferUtility.Upload(fileName, existingBucketName);
                }
                catch (AmazonS3Exception s3Exception)
                {
                    Console.WriteLine("AWS error: " + s3Exception.Message, s3Exception.InnerException);

                    //Open a file for writing
                    string errorOutputFileName = System.IO.Path.GetFullPath("C:\\amazontestdirectory\\logs\\errorLog.txt");

                    //Get a StreamWriter class that can be used to write to the file
                    if (File.Exists(errorOutputFileName))
                    {
                        StreamWriter objStreamWriter;
                        objStreamWriter = File.AppendText(errorOutputFileName);
                        objStreamWriter.WriteLine();
                        objStreamWriter.WriteLine(DateTime.Now.ToString() + " - AWS error: " + s3Exception.Message, s3Exception.InnerException);

                        //Close the stream
                        objStreamWriter.Close();
                    }
                }
            }
        }
4

1 回答 1

4

要在使用 TransferUtility 时设置权限,您需要使用接收 TransferUtilityUploadRequest 的上传覆盖。这使您可以在上传文件时设置更高级的选项。

TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
{
    BucketName = existingBucketName,
    FilePath = fileName,
    CannedACL = S3CannedACL.BucketOwnerFullControl
};

// Upload a file, file name is used as the object key name.
fileTransferUtility.Upload(request);
于 2013-11-06T06:09:15.347 回答