[英]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.