繁体   English   中英

从 Swift 上传视频到 Amazon S3

[英]Upload video to Amazon S3 from Swift

我想使用 Swift 在 Amazon S3 上上传视频,但找不到任何在线帮助。 有人能帮我吗?

谢谢!

https://docs.aws.amazon.com/en_us/aws-mobile/latest/developerguide/mobile-hub-add-aws-mobile-user-data-storage.html

1)创建一个Podfile:

platform :ios, '8.0'
inhibit_all_warnings!
use_frameworks!

target 'AmazonS3Upload' do
pod 'AWSS3'
end 
  1. 从终端运行下一个命令:

    吊舱安装

  2. 打开生成的工作区。 之后,我们可以使用 Pods 中的框架来实现文件上传。

  3. 我们需要导入2个模块:

导入 AWSS3

导入 AWSCore

  1. 使用您的凭证设置 AWS 配置。 例如:

    让 accessKey = "..." let secretKey = "..."

    让凭证提供者 = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)

    让配置 = AWSServiceConfiguration(区域:AWSRegionType.usEast1,凭证提供者:凭证提供者)

    AWSServiceManager.default().defaultServiceConfiguration = 配置

  2. 创建上传请求:

    let url = ...URL 到您的文件... let remoteName = "上传文件的名称" let S3BucketName = "您在 Amazon S3 上的存储桶名称"

    让 uploadRequest = AWSS3TransferManagerUploadRequest()! uploadRequest.body = url uploadRequest.key = remoteName uploadRequest.bucket = S3BucketName uploadRequest.contentType = "image/jpeg" uploadRequest.acl = .publicRead

  3. 并使用 AWSS3TransferManager 上传。

    let transferManager = AWSS3TransferManager.default() transferManager?.upload(uploadRequest).continue({ (task: AWSTask) -> Any? in if let error = task.error { print("Upload failed with error: ((error.localizedDescription) ))") }

    if let exception = task.exception { print("上传失败,出现异常 ((exception))") }

    if task.result != nil { let url = AWSS3.default().configuration.endpoint.url let publicURL = url?.appendingPathComponent(uploadRequest.bucket!).appendingPathComponent(uploadRequest.key!)

     print("Uploaded to:\\(publicURL)")

    }

    返回零})

您还可以按照https://aws-amplify.github.io/docs/ios/storage使用 Amplify CLI 来配置 S3,并使用 Amplify 框架的 Storage 插件与 S3 交互。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM