使用 AWS Appsync 将图像上传到 s3 的过程 || iOS 使用 Appsync 上传图片

Process for uploading image to s3 with AWS Appsync || iOS image uploading with Appsync

我正在处理一个需要以图像形式上传附件的新项目。我正在使用 DynamoDB 和 AppSync API 从数据库中插入和检索数据。由于我们不熟悉 AppSync 以及我们为应用程序使用的所有亚马逊服务和数据库,所以我对身份验证过程有点困惑。现在我们正在使用 API 密钥进行身份验证,我已经尝试了这些步骤将图像上传到 s3。

1 使用静态配置配置 AWSServiceManager,例如:-

let staticCredit =  AWSStaticCredentialsProvider(accessKey: kAppSyncAccessKey, secretKey: kAppSyncSecretKey)
let AppSyncRegion: AWSRegionType = .USEast2
let config = AWSServiceConfiguration(region: AppSyncRegion, credentialsProvider: staticCredit)
AWSServiceManager.default().defaultServiceConfiguration = config

2 使用此方法上传图片:-

func updatePictureToServer(url:URL, completion:@escaping (Bool)->Void){
    let transferManager = AWSS3TransferManager.default()
    let uploadingFileURL = url
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    let userBucket = String(format: "BUCKET")
    uploadRequest?.bucket = userBucket
    let fileName = String(format: "%@%@", AppSettings.getUserId(),".jpg")
    uploadRequest?.key = fileName
    uploadRequest?.body = uploadingFileURL
    transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
        if let error = task.error as NSError? {
            if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
                switch code {
                case .cancelled, .paused:
                    break
                default:
                    print("Error uploading: \(String(describing: uploadRequest!.key)) Error: \(error)")
                }
            } else {
                print("Error uploading: \(String(describing: uploadRequest!.key)) Error: \(error)")
            }
            completion(false)
            return nil
        }

        _ = task.result
        completion(true)
        print("Upload complete for: \(String(describing: uploadRequest!.key))")
        return nil
    })
}

3 最后我可以在 S3 存储桶上看到上传的图像

但我很关心如何保存图像 url 以及如何检索图像,因为当我必须制作存储桶 PUBLIC 来检索图像时我不知道不认为这是一个好方法,而且是否有必要拥有一个 Cognito 用户池,因为我们还没有在我们的应用程序中使用 Cognito 用户池,并且对此也不太了解,并且文档在实际情况下没有帮助,因为我们正在实施我是第一次,所以我们需要一些帮助。

所以两个问题:-

  1. 用于为 S3 和 AppSync 上传和检索图像的正确程序。
  2. 是否需要使用 Cognito 用户池进行图片上传和检索。

谢谢

Note: Any suggestion or improvement or anything related to the AppSync, S3 or DynamoDB will be truly appreciated and language is not a barrier just looking for directions so swift or objective-c no problem.

您需要使用 Cognito Federated Identities 为每个用户提供他们自己的安全存储桶的存储桶的每个身份安全性。您可以利用 AWS Amplify 为您的项目进行设置 $amplify add auth 并选择默认配置,然后 $amplify add storage 为该存储桶和池配置适当的权限以使用私有上传。

有关更多信息,请查看存储库:https://github.com/aws-amplify/amplify-cli