SlickUpload
Welcome Guest Search | Active Topics | Log In | Register

Can I specify folder in S3 bucket? Options
imsam67
#1 Posted : Monday, November 28, 2011 1:11:00 PM
Rank: Member

Groups: Registered

Joined: 11/22/2011
Posts: 17
Location: Florida

I'm using SlickUpload to upload files to my S3 account. Two questions:

 

  1. Is there a way for me to specify a folder name within my bucket? If this is possible, a follow up question is whether I can create that folder if it doesn't already exist in my bucket?
  2. Is there a way I can specify a different bucket name programmatically OR do I always have to use the bucket specified in web.config?

    I have multiple buckets in my S3 account and I'd like to save files in different buckets depending on some business logic in my code.

 


Thanks,
Sam
Axosoft
#2 Posted : Monday, November 28, 2011 1:34:06 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

You can do both with the custom uploadstreamprovider code I suggested here: http://krystalware.com/forums/yaf_postst1562_How-do-I-rename-a-file-before-uploading-it.aspx.

To specify the folder name, just return it as part of the object name you return from GetObjectName (for example, returning "folder/file.ext"). S3 doesn't have the concept of folders per se, so it doesn't need to create them -- it just allows you to delineate files based on the "/" character in their name.

Changing the bucket name is a bit more complex, because the built-in provider assumes you'll be storing to just one bucket. Still, you just need to override the appropriate sections of code in the provider to create your own implementation. Something like this:

    public class CustomS3UploadStreamProvider : S3UploadStreamProvider
    {
        string _accessKeyId;
        string _secretAccessKey;
 
        public CustomS3UploadStreamProvider(UploadStreamProviderElement settings)
            : base(settings)
        {
            accessKeyId = Settings.Parameters["accessKeyId"];
            secretAccessKey = Settings.Parameters["secretAccessKey"];
        }
 
        public virtual string GetObjectName(UploadedFile file)
        {
            // TODO: return the correct object name (including path if desired)
        }
 
        public virtual string GetClientFor(UploadedFile file)
        {
            // TODO: specify the correct bucket name, based on biz logic;
            string bucketName = "CHANGEME";
 
            // TODO: maybe cache this?
            return new S3BlobClient(accessKeyId, secretAccessKey, bucketName);
        }
 
        public override Stream GetWriteStream(UploadedFile file)
        {
            S3BlobInfo blobInfo = CreateBlobInfo(file);
 
            file.ServerLocation = blobInfo.Name;
 
            return GetClientFor(file).GetPutBlobStream(blobInfo);
        }
 
        public override void RemoveOutput(UploadedFile file)
        {
            GetClientFor(file).DeleteBlob(file.ServerLocation);
        }
 
        public override Stream GetReadStream(UploadedFile file)
        {
            return GetClientFor(file).GetBlob(file.ServerLocation);
        }
    }

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.