Search code examples
amazon-web-servicesamazon-s3awss3transferutility

Upload subdirectory transferutility S3


I want to upload all the files/folder inside a directory to a S3 bucket. I want to upload including the all the files in all the sub directories. I thought of using TransferUtility to do this. Though the link here says that 'By default, Amazon S3 only uploads the files at the root of the specified directory. You can, however, specify to recursively upload files in all the subdirectories.' but I couldnt find a way to do this. I coundnt find any property where I can mention to include all the sub directories. I tried using SearchOption = System.IO.SearchOption.AllDirectories and SearchPattern = "*" to achieve this, but still it uploaded only the files in the top most directory. Please help me in this. Thanks.

I'm using the below code,

TransferUtility directoryTransferUtility = new TransferUtility(s3Client);

                TransferUtilityUploadDirectoryRequest uRequest = new TransferUtilityUploadDirectoryRequest()
                {
                    Directory = dirPath,
                    BucketName = bucketName,
                    SearchOption = System.IO.SearchOption.AllDirectories,
                    SearchPattern = "*"
                };

                directoryTransferUtility.UploadDirectory(dirPath, bucketName);

Solution

  • This is what worked for me: I set the options on the UploadDirectory method and used "*.*" as the search pattern.

        directoryTransferUtility.UploadDirectory(dirPath, 
                                                 bucketName,
                                                 "*.*",
                                                 SearchOption.AllDirectories);