我正在尝试通过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;
}
}
答案 0 :(得分:0)
尝试使用此功能在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
})
}