作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过我的 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
})
}
关于ios - 通过 iOS 将视频上传到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56371105/
我是一名优秀的程序员,十分优秀!