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: "http://s3.amazonaws.com/\(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;
}
and for uploading image use
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;
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With