0

I am trying to move data for example;

Source = "Uploads/Photos/" to Destination="Uploads/mytest/"

I am getting error like that but but when i give a specific file this works.

Basically, I want to move folder with all files.

My code is below;

        public async Task<MoveResponse> MoveObject(MoveRequest moveRequest)
        {
            MoveResponse moveResponse = new MoveResponse();

            CopyObjectRequest copyObjectRequest = new CopyObjectRequest
            {
                SourceBucket = moveRequest.BucketName,
                DestinationBucket = moveRequest.BucketName + "/" + moveRequest.Destination,
                SourceKey = moveRequest.Source,
                DestinationKey = moveRequest.Source,
            };

            var response1 = await client.CopyObjectAsync(copyObjectRequest).ConfigureAwait(false);

            if (response1.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                moveResponse.IsError = true;
                moveResponse.ErrorMessage = "Files could not moved to destination!";

                return moveResponse;
            }      

            return moveResponse;
        }
Mehmet
  • 65
  • 1
  • 7
  • 1
    You cannot move a folder, you can only move individual files and you *need to* move all individual files. – luk2302 Jun 20 '22 at 08:12
  • Do you have any example related to topic? If you have could you share with me, please? @luk2302 – Mehmet Jun 20 '22 at 08:56
  • You can move folder to S3. I have updated sample code right below. it may help you! @Mehmet – Santosh Kokatnur Jun 20 '22 at 10:58
  • Your code would need to `ListObjects` to obtain a list of objects to 'move'. Then, loop through each object and use `CopyObject` to copy it to the new location, then `DeleteObject` to delete the object from the original location. – John Rotenstein Jun 21 '22 at 00:52
  • Thank you your answer is logical. I get it, but there has to be an easier way. @John Rotenstein – Mehmet Jun 21 '22 at 05:39
  • If you want "easier", then you could use the AWS CLI (which makes the listing/copying/deleting API calls for you). Here's an old example in .Net: [Best way to move files between S3 buckets?](https://stackoverflow.com/a/14812248/174777) – John Rotenstein Jun 21 '22 at 05:59

1 Answers1

1

I hope, you are using high level S3 API's.

Check out this sample code

private void uploadFolderToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string directoryPath = textBoxBasePath.Text + listBoxFolder.SelectedItem.ToString().Replace("[", "").Replace("]", "");
        string bucketName = comboBoxBucketNames.Text;
        string FolderName = listBoxFolder.SelectedItem.ToString().Replace("[", "").Replace("]", "");
        try
        {
            TransferUtility directoryTransferUtility = new TransferUtility(new AmazonS3Client(AwsAccessKeyID, AwsSecretAccessKey, RegionEndpoint.USEast1));

            TransferUtilityUploadDirectoryRequest request = new TransferUtilityUploadDirectoryRequest
            {
                BucketName = bucketName,
                KeyPrefix = FolderName,
                StorageClass = S3StorageClass.StandardInfrequentAccess,
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
                Directory = directoryPath,                    
                SearchOption = SearchOption.AllDirectories,
                SearchPattern = "*.*",                    
                CannedACL = S3CannedACL.AuthenticatedRead
            };
            ListMultipartUploadsRequest req1 = new ListMultipartUploadsRequest
            {
                BucketName = bucketName
            };

            var t = Task.Factory.FromAsync(directoryTransferUtility.BeginUploadDirectory, directoryTransferUtility.EndUploadDirectory, request, null);
            t.Wait();

            MessageBox.Show(string.Format("The Directory '{0}' is successfully uploaded", FolderName));
        }
catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        { }
    }
Santosh Kokatnur
  • 357
  • 3
  • 9
  • 19