Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload a file/directory to a folder within a bucket?

Tags:

c#

.net

amazon-s3

I'm able to upload files or directories to a bucket with the AWS .NET SDK, but they always end up in the root folder.

Is there a way to upload a file to an existing directory?

  • edit. More info:

So I'm using a TransferUtilityUploadDirectoryRequest to upload a directory from my local disk to S3. I would like the files to be uploaded to a folder in the bucket with the same name as the folder I've selected.

For example. if I choose the directory c:/stuff to be upload, I want the contents of c:/stuff to go in BucketName/stuff, not directly into the bucket.

I hope it's clear what I'm trying to do, if not I'll try to provide more info

like image 531
topher-j Avatar asked May 25 '11 13:05

topher-j


People also ask

Is bucket same as folder?

There are a few key terms to understanding AWS S3, the first of which is the “Bucket”. A Bucket is a logical container of objects. In traditional NAS terms, this would be a “folder”, but because S3 deals with objects and not files, the distinction becomes important.

Can S3 buckets have folders?

You can have folders within folders, but not buckets within buckets. You can upload and copy objects directly into a folder.

What is multi Part upload?

Multipart upload allows you to upload a single object as a set of parts. Each part is a contiguous portion of the object's data. You can upload these object parts independently and in any order. If transmission of any part fails, you can retransmit that part without affecting other parts.

How do I add a file to a bucket?

In the Google Cloud console, go to the Cloud Storage Buckets page. In the list of buckets, click on the name of the bucket that you want to upload an object to. In the Objects tab for the bucket, either: Drag and drop the desired files from your desktop or file manager to the main pane in the Google Cloud console.

What is the command to copy files recursively in a folder to an S3 bucket?

s3://destination --recursive . That way, it is copying the contents of the current directory.


2 Answers

It seems after googling around, you specify a key. It took me a while, but I believe the key is something like this example:

string key = string.Format("{0}/{1}", folder, filename); 
PutObjectRequest rq = new PutObjectRequest()
{
    AutoCloseStream = false,
    BucketName = s3BucketName,
    InputStream = stream,
    Key = key
};

S3ClientInstance.PutObject(rq).Dispose();
like image 139
Chuck Savage Avatar answered Oct 01 '22 08:10

Chuck Savage


The newest version of the AWS SDK for .NET allows you to set the KeyPrefix property on the UploadDirectoryRequest (more info here).

like image 27
Jeff Barr Avatar answered Oct 01 '22 07:10

Jeff Barr