Upload content to S3 through CloudFront

。_饼干妹妹 提交于 2019-12-07 12:20:56

问题


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


回答1:


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




回答2:


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);


来源:https://stackoverflow.com/questions/15559203/upload-content-to-s3-through-cloudfront

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!