I am new to AWS and using it for iOS app.
I'm trying to upload images from my iOS app to bucket named, "img.haraj.com.sa". When I upload any image, they aren't shown in bucket. But when I change the target to bucket named "haraj", they are uploaded and shown in the bucket.
Here's the policy:
{
"Statement": [
{
"Sid": "**********hidden**********",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::haraj/*"
]
}
]
}
I modify this for changing the target bucket. I also created other buckets with name "img1.haraj.com.sa" and tried uploading images and unfortunately they also failed.
It seems there is some problem with having bucket names with dots (.) and without dots. Names of buckets without dots works with iOS app and the names with dots doesn't work. I'm not sure though. But I'm facing this problem. I don't receive any error response in app code.
Here's part of my iOS app implementation:
- (void)postAdButtonPushed:(id)sender
{
DLog(@"Post Ad")
AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:AWS_ACCESS_KEY_ID withSecretKey:AWS_SECRET_KEY];
s3Client.timeout = 240;
NSString *bucketName = [NSString stringWithFormat:@"img.haraj.com.sa"];
NSString *imageName = [NSString stringWithFormat:@"testimage.jpg"];
S3PutObjectRequest *objReq = [[S3PutObjectRequest alloc] initWithKey:imageName inBucket:bucketName];
objReq.contentType = @"image/jpeg";
UIImage *testImageToUpload = [self.imagesToUpload objectAtIndex:0];
NSData *imageData = UIImageJPEGRepresentation(testImageToUpload, 0.8);
objReq.data = imageData;
objReq.delegate = self;
objReq.contentLength = [imageData length];
[s3Client putObject:objReq];
}
- (void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
DLog(@"response: %@", response.description)
}
- (void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error
{
DLog(@"Req failed: %@", error.description)
}
I also created a thread on Amazon Forum at: AWS Upload image to Bucket iOS app
Any help would be appreciated. Thank you!
I've posted a response to your forum thread here, but to summarize, I believe you've bumped up against a bug in the SDK and will need to explicitly set the S3 endpoint where your bucket is located.
Just wanted to weigh in, here is a code snippet that i got working
// #import <AWSS3/AWSS3.h>
// #import <AWSRuntime/AWSRuntime.h>
// then you should implement <AmazonServiceRequestDelegate>
// import those in your .h file and
// add the awss3 and awsruntime framework from the client
// download from Amazon
// myFace is the UIImage object
AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:@"Key_Goes_here" withSecretKey:@"Secret_Goes_Here"];
NSString *imageName = [NSString stringWithFormat:@"%@.png", @"cpa"];
S3PutObjectRequest *objReq = [[S3PutObjectRequest alloc] initWithKey:imageName inBucket:@"bucket_name"];
objReq.contentType = @"image/png";
objReq.cannedACL = [S3CannedACL publicRead];
objReq.data = UIImagePNGRepresentation(myFace);
objReq.delegate = self;
[s3Client putObject:objReq];
here are the delegate methods:
-(void)request:(AmazonServiceRequest *)request didSendData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite {
}
-(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"response! %@", response);
}
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response {
}
-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data {
NSLog(@"data?");
}
-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error {
[self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Upload Error"];
}
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