0

So, I have an Amazon S3 encoded URL that I want to download. If I pass the URL along without quotes, I get a 403 Forbidden response from S3.

curl -O https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/t/20141008/413f96bd17a210c0cfe2997d83a4782c/Nerve_Flossing_Highly_Irratible_Radial_Nerve_Wartenburgs_Syndrome-thumb.jpg?AWSAccessKeyId=AKIAI456JQ76GBU7FECA&Signature=Sd18YXJIMvLqeRFZYCzDNYdpg7U%3D&Expires=1412866120

But, just wrapping it in quotes, works great from the command line:

curl -O "https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/t/20141008/413f96bd17a210c0cfe2997d83a4782c/Nerve_Flossing_Highly_Irratible_Radial_Nerve_Wartenburgs_Syndrome-thumb.jpg?AWSAccessKeyId=AKIAI456JQ76GBU7FECA&Signature=Sd18YXJIMvLqeRFZYCzDNYdpg7U%3D&Expires=1412866120"

But... when I try to implement this in PHP, I end up saving a file with 0 bytes.

$curl = curl_init("'".$url."'");
$fp = fopen($filepath, "w");
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_exec ($curl);
curl_close ($curl);
fclose($fp);

Is there a way to do this with PHP that I'm missing?

EDIT: to provide the actual S3 url for easier testing.

3 Answers 3

0

PHP curl is simply expecting a parameter. When you add quotes on the command line, you're asserting that whatever lies in between the quotes is the specific parameter you're trying to pass into the command. Most command line commands work this way. For instance, if you wanted to move a file in command line, you can add the path in quotes:

mv foo.txt "/home/bar/foo.txt"

This is great for complex parameters (such as URLs), files with spaces in the names, etc.

As for PHP, the curl function is simply expecting a parameter and it will do the proper escaping internally. So when you add quotes into the curl PHP command, you're actually asking PHP to load this URL:

'https://some.amazon.bucket.s3.amazonaws.com/o/123456/582305890298509238/my-video.mp4?AWSAccessKeyId=RANDOMKEYHERE&Signature=SIG&Expires=1412805904'

As opposed to:

https://some.amazon.bucket.s3.amazonaws.com/o/123456/582305890298509238/my-video.mp4?AWSAccessKeyId=RANDOMKEYHERE&Signature=SIG&Expires=1412805904

You mentioned that using no quotes doesn't work in command line curl. Have you tried adding no quotes in PHP? My guess is that this will work.

One other thing to be weary of is your S3 bucket's permissions. It's possible the file you're attempting to load from S3 doesn't have the correct access permissions that would allow PHP or your web server to access it. This would trigger a 403 error.

1
  • Yep, I've tried it both ways. Doubles quotes, single quotes, no quotes. I also added the real URL to the asset on S3 to hopefully help with the problem... Also, the bucket isn't mine, it's a bucket maintained by Zencoder for temp files.
    – jpea
    Commented Oct 8, 2014 at 16:38
0

Looks like that is not quotes. Quotes are absolutely not important in this case.

Looks like something is wrong with SSL certs. I see you SSL certificate is not good. Even, it is very bad.

You need to turn verification off:

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

UPDATE

This code does the trick:

$curl = curl_init('https://some.amazon.bucket.s3.amazonaws.com/o/123456/582305890298509238/my-video.mp4?AWSAccessKeyId=RANDOMKEYHERE&Signature=SIG&Expires=1412805904');
$fp = fopen(getcwd() . '/1.txt', "w");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
fwrite($fp, curl_exec($curl));
fclose($fp);
curl_close($curl);

I have checked this

I still think your main problem is SSL cert...

By the way file contents is:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><RequestId>6DCACEEE9E886AF1</RequestId><HostId>JPHAbX6+Ql34/6Yvso0vofODswNtZvwPluJj3R6XBBsSkq0hjQ9cy/Aj6e73EOQt</HostId><AWSAccessKeyId>RANDOMKEYHERE</AWSAccessKeyId></Error>
1
  • It still saves a file with 0 bytes. And it works without any options on the command line except -O. Just not in PHP.
    – jpea
    Commented Oct 8, 2014 at 16:14
-1

Your url contain various special character and the whole string putting to a single character thatswhy we use the double quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.