[英]Upload video to Amazon S3 via iOS
我正在尝试通过我的iOS应用程序将视频上传到Amazon S3。 我很难使用AWS提供的文档。
首先,如果我想将.mov视频上传到S3,contentType应该是什么? 我似乎无法找到任何我可以上传的内容格式的文档。
其次,如何将我的.mov视频作为Data()对象传递?
func uploadData() {
let data: Data = Data() // Data to be uploaded
let expression = AWSS3TransferUtilityMultiPartUploadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
// Do something e.g. Update a progress bar.
})
}
var completionHandler: AWSS3TransferUtilityMultiPartUploadCompletionHandlerBlock
completionHandler = { (task, error) -> Void in
DispatchQueue.main.async(execute: {
// Do something e.g. Alert a user for transfer completion.
// On failed uploads, `error` contains the error object.
})
}
let transferUtility = AWSS3TransferUtility.default()
transferUtility.uploadUsingMultiPart(data:data,
bucket: "YourBucket",
key: "YourFileName",
contentType: "text/plain",
expression: expression,
completionHandler: completionHandler).continueWith {
(task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with uploadTask.
}
return nil;
}
}
尝试使用此功能在AWS中上传图像/视频
public struct AWSConstant {
static let COGNITO_POOL_ID = "us-west-2:4918c1f8-d173-4668-8891-d6892a147259"
static let BUCKET_NAME = "spinach-cafe/main-image"
static let baseUrl = "https://s3-us-west-2.amazonaws.com/"
}
func uploadVideoToAmazon(currentFileName: String, videoFileURL: URL, contentType: String = "image/jpeg", _ bucket: String?, progressBlock: @escaping (Float) -> () = { _ in }, callBack: @escaping (_ url: String) -> Void?, failedBlock: @escaping (Error?) -> () = { _ in }) {
let myIdentityPoolId = AWSConstant.COGNITO_POOL_ID
let credentialsProvider: AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: .USWest2, identityPoolId: myIdentityPoolId)
let endpoint = AWSEndpoint.init(region: .USWest2, service: .S3, useUnsafeURL: false)
let configuration = AWSServiceConfiguration.init(region: .USWest2, endpoint: endpoint, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
var S3BucketName = AWSConstant.BUCKET_NAME
if let bucketName = bucket {
S3BucketName = bucketName
}
let remoteName = currentFileName
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.body = videoFileURL
uploadRequest?.key = remoteName
uploadRequest?.bucket = S3BucketName
uploadRequest?.contentType = contentType
uploadRequest?.acl = .publicRead
uploadRequests.append(uploadRequest)
uploadRequest?.uploadProgress = { bytesSent, totalBytesSent, totalBytesExpectedToSend in
DispatchQueue.main.async(execute: {
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
let transferManager = AWSS3TransferManager.default()
// Perform file upload
transferManager.upload(uploadRequest!).continueWith(block: { task -> Any? in
if let error = task.error {
print("Upload failed with error: (\(error.localizedDescription))")
}
if task.result != nil {
let stringURL = "\(AWSConstant.baseUrl)\(S3BucketName)/\(currentFileName)"
print("Uploaded to:\n\(stringURL)")
// Remove locally stored file
DispatchQueue.main.async() {
callBack(stringURL)
}
}
else {
DispatchQueue.main.async() {
failedBlock(task.error)
}
print("Unexpected empty result.")
}
return nil
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.