VICTORGS3
VICTORGS3

Reputation: 203

Upload content to S3 through CloudFront

I'm working on an app which requires to upload images to S3. Now I am doing like this:

S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:nameOfThePicture inBucket:nameOfTheBucket];
    por.contentType = @"image/jpg";
    por.data        = imageData;

    // Put the image data into the specified s3 bucket and object.
    S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];
    por.delegate = self;

Uploading an image directly to S3 is too slow and I have configured the CloudFront service. Now, I'd like to upload an image through CloudFront to the origin bucket in S3. Is possible to do it? If yes, how could I do it?

Really thanks,

Victor

Upvotes: 4

Views: 1661

Answers (2)

Mangesh Borkar
Mangesh Borkar

Reputation: 71

I had the same requirement in my project. Although I myself could not figure out everything, there is a link I came across.

http://raamdev.com/2008/using-curl-to-upload-files-via-post-to-amazon-s3/

I tried to mimic the same with PHP curl, but somehow I see that the key is uploaded, rather than image file. May be this code will provide you pointers, if anybody is able to figure out issue in the below script, it will be great help for me as well as anybody wanting to achieve this.

$url = 'http://xxxxxxxxxxx.cloudfront.net/'; // cloudfront distribution url.

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 

//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("key"=>$_REQUEST['key'],"file"=>$_FILES['file']['tmp_name']));


//curl_setopt($ch, CURLOPT_POSTFIELDS, array("key"=>$_FILES['file']['tmp_name']));
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

if (false == $result) {
    echo "Error while loading page: ", curl_error($ch), "\n"; 
}
curl_close($ch);
var_dump($result);

Upvotes: 0

user149341
user149341

Reputation:

No. CloudFront is a one-way affair — you can retrieve objects from S3 through CloudFront, but you cannot upload objects to S3 through CloudFront.

Upvotes: 5

Related Questions