简体   繁体   English

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

[英]Process for uploading image to s3 with AWS Appsync || iOS image uploading with Appsync

I'm working on a new project that requires uploading attachments in the form of images. 我正在开发一个新项目,需要以图像的形式上传附件。 I'm using DynamoDB and AppSync API's to insert and retrieve data from database. 我正在使用DynamoDB和AppSync API来插入和检索数据库中的数据。 As we are new to the AppSync and all the amazon services and database we are using for the app i'm little bit confused about the authentication process. 由于我们是AppSync的新手以及我们用于应用程序的所有亚马逊服务和数据库,我对身份验证过程有点困惑。 Right now we are using API key for authentication and I have tried these steps to upload image to s3. 现在我们正在使用API​​密钥进行身份验证,我已尝试将这些步骤上传到s3。

1 Configue the AWSServiceManager with static configuration like :- 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 Uploading picture with this method : - 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 And finally i'm able to see the uploaded image on the S3 bucket 3最后我能够在S3存储桶上看到上传的图像

在此输入图像描述

But i'm concerned about how to save the url of the image and how to retrieve the image because when i have to make the buket PUBLIC to retrieve the image and i don't think that's a good approach, plus is it necessary to have a Cognito user pool because we aren't using Cognito user pool yet in our app and not have much knowledge about that too and documents are not helping in practical situations because we are implementing ti for the first time so we need some little help. 但我担心如何保存图像的网址以及如何检索图像,因为当我必须使用PUBLIC来检索图像时我不认为这是一个好方法,加上是否有必要Cognito用户池,因为我们还没有在我们的应用程序中使用Cognito用户池,也没有太多的知识,文档在实际情况下没有帮助,因为我们是第一次实现ti所以我们需要一些帮助。

So two question : - 所以有两个问题: -

  1. Proper procedure to use for uploading and retrieving images for S3 and AppSync. 用于上传和检索S3和AppSync图像的正确过程。
  2. Is it necessary to use Cognito user pool for image uploading and retrieving. 是否有必要使用Cognito用户池进行图像上载和检索。

Thanks 谢谢

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. 注意:任何建议或改进或与AppSync,S3或DynamoDB相关的任何内容都将得到真正的赞赏,语言不是一个障碍,只是寻找方向如此迅速或客观 - 没有问题。

You need per-identity security on the bucket using Cognito Federated Identities which gives each user their own secure bucket. 您需要使用Cognito Federated Identities为存储桶提供每个身份的安全性,这为每个用户提供了自己的安全存储桶。 You can leverage the AWS Amplify to set this up for your project with $amplify add auth and selecting the default config, then $amplify add storage which configures that bucket and pool with appropriate permissions to use private uploads. 您可以利用AWS Amplify为您的项目设置此项,使用$amplify add auth并选择默认配置,然后使用$amplify add storage来配置具有适当权限的$amplify add storage桶和池以使用私有上载。

For more info checkout the repo: https://github.com/aws-amplify/amplify-cli 有关更多信息,请查看repo: https//github.com/aws-amplify/amplify-cli

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

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