2013-11-01 125 views
1

我試圖使用.NET SDK將文件上傳到Amazon S3。上傳工作正常,除了它將default permission應用於上傳的文件,這是'私人'。使用非默認權限將文件上傳到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

要在使用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); 
+0

是的,經過更多的研究,我發現我不得不使用TransferUtilityUploadRequest。現在所有人都在工作。 –