gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:25 39 4
gpt4 key购买 nike

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

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

    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: "http://s3.amazonaws.com/\(S3BucketName)/\(uploadRequest.key!)")!
print("Uploaded to:\n\(s3URL)")
}
else {
print("Unexpected empty result.")
}
return nil
}

最佳答案

如果没有 cognito,请在

- (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;
}];

关于ios - 如何在没有 Amazon Cognito 的情况下使用 AWS S3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447433/

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