使用amazon s3存储桶的AWS Sync文件

时间:2018-06-09 20:18:20

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

在linux中,我可以同步这样的文件:

https://serverfault.com/questions/682708/copy-directory-structure-intact-to-aws-s3-bucket

现在在使用c#的Windows上,这是我上传文件的方式:

using (var fileTransferUtility = new TransferUtility(_credentials.AccessKeyId, _credentials.SecretAccessKey, _region))
{
    using (FileStream fileToUpload = new FileStream(fileLocation, FileMode.Open, FileAccess.Read))
    {
           var fileTransferUtilityRequest = new TransferUtilityUploadRequest
           {
                                BucketName = bucketName,

                                InputStream = fileToUpload,
                                StorageClass = S3StorageClass.ReducedRedundancy,
                                Key = key,
                                CannedACL = S3CannedACL.PublicRead,
            };

           fileTransferUtility.Upload(fileTransferUtilityRequest);   
    }
}

如何同步目录而不是仅使用c#上传文件?

1 个答案:

答案 0 :(得分:1)

以下是使用C#,

将目录上传到S3的方法
try
    {
        TransferUtilityUploadDirectoryRequest request = new TransferUtilityUploadDirectoryRequest
        {
            BucketName = bucket,
            Directory = uploadDirectory,
            SearchOption = System.IO.SearchOption.AllDirectories,
            CannedACL = S3CannedACL.PublicRead
        };
        _transferUtility.UploadDirectory(request);
        return true;
    }
    catch (Exception exception)
    {
        //Log Exception
        return false;
    }

可以在此博客中找到更多示例

https://www.samuelnmensah.com/blog/upload-delete-entire-directory-amazon-s3-using-transfer-utility/

谷歌链接的缓存版本:

http://webcache.googleusercontent.com/search?q=cache:b1CN7MxwkLwJ:samuelnmensah.com/uploading-and-deleting-an-entire-directory-to-aws-s3-using-transfer-utility/+&cd=1&hl=en&ct=clnk&gl=us

希望它有所帮助。