简体   繁体   中英

AWS Sync files with amazon s3 bucket

In linux I am able to sync files like this:

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

Now on windows using c# this is how I upload a file:

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);   
    }
}

How can I sync a directory instead of just uploading a file using c#?

Here is how you upload a directory to S3 using C#,

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;
    }

More examples can be found from this blog,

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

Cached version of the link from google:

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM