简体   繁体   中英

IOS Upload Images To AWS 3 Generate Random Unique Key Names

I am currently using Amazon S3 to upload images from an IOS app with Cognito Identities and a custom back-end server.

When uploading the image, a key needs to be set for the image URL

_uploadRequest.key = @"filename.jpg";

The final image URL will be:

http://s3.amazonaws.com/bucketname/filename.jpg

Is there a good way to generate unique random strings in IOS in order to have unique filenames or would it be to generate them server side (Rails back-end) ?

Well you can 1. Append current date and time to filename(but it may fail if you create some filename at once)

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd he:mm:ss"
let key1 = "filename_" + dateFormatter.stringFromDate(NSDate()) + ".jpg"

2. Use NSUUID and getting string representation from it

let key2 = "filename_" + NSUUID().UUIDString + ".jpg"

3. And other ways as well

file name should be generated from code end only and for that you can make any specific format as like the live DateandTime to string for specific timed image. which may help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM