davidbeere
Occasional Observer

Passing S3 bucket meta data

So here is the situation, we are uploading assets to an S3 bucket using the standard built in CrownPeak features and this works for the images that we then want to reference directly on the external sites. However, we want to also upload some images and pdfs that work fine for the upload, but we want them to download when the user clicks on them rather than just open as usual. Apparently, (according to https://stackoverflow.com/a/6007788) we need to send through some meta data to S3 with the asset. I can't see how to do this, anyone tried this before or can point me in the right direction?
0 Kudos
2 Replies
kunalshah
Partner

You are correct, we need to add additional metadata to object that is being uploaded.

 

Content-Disposition: attachment;
filename="image.jpg"

Three options here -
1) Do it at the time of object creation
2) Use copy_object function to copy the object to itself with the new metadata.
3) Use the s3 console and manually manage addition of new metadata. (This can be insane if your files are being managed from the CMS, so practically, this is not an option)

 

I did not find any direct way where we can tweak s3 publising in CP's publishing packges, but you can employ any kind of a post-publish (PostPublishContext) event to do a copy_object function on the s3 object to itself - and add the new meta info then. You can try to employ some kind of an Asset Wrapper Template for the same.

Here's how you can attache the file on the asset wrapper template

//Attach file to the requested asset
AssetAttachRequest AttachReq = new AssetAttachRequest();
AttachReq.assetId = attachAssetId;
AttachReq.originalFilename = fileName;
AttachReq.bytes = File.ReadAllBytes(fileName);
//Renames the original filename
AttachReq.originalFilename = desiredUploadName;
AssetAttachResponse resp = accessAsset.Attach(AttachReq);

You will also need to create an intermediate web api or similar that you can consume inside the post publish event - as you will not be able to directly use the s3 sdk inside the template code.

@kunalshahthanks for the response, I'll give that a go and see if I can get it working.
0 Kudos