如何在没有Amazon Cognito的情况下使用AWS S3?

编程入门 行业动态 更新时间:2024-10-09 20:23:35
本文介绍了如何在没有Amazon Cognito的情况下使用AWS S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Node.js REST API来验证我的用户.通过身份验证后,我允许他们发布带有文字的照片.我的计划是将文字和照片的URL存储在数据库中.这样,当他们转到帖子的提要时,我的应用程序将查询数据库以获取文本和URL,然后使用所有URL直接从S3获取图像.这是这样做的正确方法吗?如果是这样,我怎么不使用cognito就无法做到这一点.我正在尝试削减成本,由于我已经使用API​​添加了身份验证,因此cognito似乎毫无用处.

I am using a Node.js REST API to authenticate my users. Once they are authenticated I allow them to post photos with text. My plan is to store the text and the URL to the photo in the database. That way when they go to the feed of posts, my app will query the database to get the text and URL's and then use all of the URL's to get the images from S3 directly. Is this the correct way to do it and if so how come I can not do so without using cognito. I am trying to cut costs and it seems like cognito would be useless since I already add authentication with my API.

这是我到目前为止的代码.

Here is the code I have thus far.

let S3BucketName = "*******" // configure authentication with Cognito let CognitoPoolID = "*************" let Region = AWSRegionType.USEast1 let credentialsProvider = AWSCognitoCredentialsProvider(regionType:Region, identityPoolId:CognitoPoolID) let configuration = AWSServiceConfiguration(region:Region, credentialsProvider:credentialsProvider) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration let ext = "png" let imageURL = NSBundle.mainBundle().URLForResource("iimage", withExtension: ext)! let uploadRequest = AWSS3TransferManagerUploadRequest() uploadRequest.body = imageURL uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext uploadRequest.bucket = S3BucketName uploadRequest.contentType = "image/" + ext let transferManager = AWSS3TransferManager.defaultS3TransferManager() transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in if let error = task.error { print("Upload failed (\(error))") } if let exception = task.exception { print("Upload failed (\(exception))") } if task.result != nil { let s3URL = NSURL(string: "s3.amazonaws/\(S3BucketName)/\(uploadRequest.key!)")! print("Uploaded to:\n\(s3URL)") } else { print("Unexpected empty result.") } return nil }

推荐答案

在没有认知的情况下用于

For without cognito use this in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:AWS_ACCESS_KEY secretKey:AWS_SECRET_KEY]; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionAPSoutheast1 credentialsProvider:credentialsProvider]; AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration; }

并用于上传图片

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager]; AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new]; uploadRequest.bucket = AWS_S3_BUCKET_NAME; uploadRequest.key = @"cards/image.png"; uploadRequest.contentType = @"image/png"; uploadRequest.body = imageURL; [[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) { if (task.error) { if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) { switch (task.error.code) { case AWSS3TransferManagerErrorCancelled: case AWSS3TransferManagerErrorPaused: break; default: NSLog(@"Error: %@", task.error); break; } } else { // Unknown error. NSLog(@"Error: %@", task.error); } } if (task.result) { AWSS3TransferManagerUploadOutput *uploadOutput = task.result; NSLog(@"success: %@", uploadOutput); } return nil; }];

更多推荐

如何在没有Amazon Cognito的情况下使用AWS S3?

本文发布于:2023-11-28 08:22:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   如何在   Amazon   Cognito   AWS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!