简体   繁体   English

IOS将图像上传到AWS 3生成随机唯一键名

[英]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. 我目前正在使用Amazon S3从具有Cognito身份和自定义后端服务器的IOS应用程序上传图像。

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) ? 是否有一种很好的方法可以在IOS中生成唯一的随机字符串以具有唯一的文件名,或者是在服务器端生成它们(Rails后端)?

Well you can 1. Append current date and time to filename(but it may fail if you create some filename at once) 好吧,您可以1.将当前日期和时间附加到文件名中(但是如果您一次创建一些文件名,则可能会失败)

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 2.使用NSUUID并从中获取字符串表示形式

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

3. And other ways as well 3.以及其他方式

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. 文件名仅应从代码结尾生成,为此,您可以将任何特定格式(例如实时DateandTime)设置为特定定时图像的字符串。 which may help. 这可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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