Amazon S3 upload with public permissions

前端 未结 5 1719
再見小時候
再見小時候 2021-02-01 03:15

I\'m using the Amazon C# SDK and trying to upload a file, but by default it has restricted permissions. I would like to make it publicly available, but I can\'t seem to find ou

5条回答
  •  一向
    一向 (楼主)
    2021-02-01 03:57

    The solution by Tahbaza is correct, but it doesn't work in the later versions of AWS SDK (2.1+)...

    Consider using the following for 2.1+ versions of SDK:

    private void UploadFileToS3(string filePath)
    {
        var awsAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
        var awsSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
        var existingBucketName = ConfigurationManager.AppSettings["AWSBucketName"];
        var client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey,RegionEndpoint.USEast1);
    
        var uploadRequest = new TransferUtilityUploadRequest
        {
            FilePath = filePath,
            BucketName = existingBucketName,
            CannedACL = S3CannedACL.PublicRead
        };
    
    var fileTransferUtility = new TransferUtility(client);
        fileTransferUtility.Upload(uploadRequest);
    } 
    

提交回复
热议问题