gpt4 book ai didi

ios - 通过 iOS 将视频上传到 Amazon S3

转载 作者:行者123 更新时间:2023-11-29 05:43:16 24 4
gpt4 key购买 nike

我正在尝试通过我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com