i need expiring downloadlinks for a bucket in eu-central-1.
With $s3->getObjectUrl($bucket, $filename, '+5 minutes'); from the SDK-PHP i only getting unsinged URLs like http://.../file.txt without the querystring.
Complete Code:
require 'vendor/autoload.php';
use Aws\S3\S3Client;
$client = S3Client::factory([
'version' => 'latest',
'region' => 'eu-central-1',
'signature' => 'v4',
'credentials' => [
'key' => 'xxxxx',
'secret' => 'xxxxx'
]
]);
$plainUrl = $client->getObjectUrl('mybucket', 'data.txt');
// > https://my-bucket.s3.amazonaws.com/data.txt
$signedUrl = $client->getObjectUrl('mybucket', 'data.txt', '+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt
I don't know why?
Ahhhh,
getObjectUrl no longer has a third parameter.
This Works:
$cmd = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => 'test1G.dat'
]);
$request = $client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();
Related
This PHP function generates a S3 pre-signed URL:
public function generatePreSignedUrl(): string
{
$secretAccessKey = urlencode(env('AWS_SECRET_ACCESS_KEY'));
$bucket = env('AWS_BUCKET');
$client = new S3Client([
'version' => 'latest',
'region' => env('AWS_DEFAULT_REGION'),
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => $secretAccessKey,
],
]);
$expires = Carbon::now()->addMinutes(15)->timestamp;
$keyname = 'video.mp4';
$command = $client->getCommand('PutObject', [
'Bucket' => $bucket,
'Key' => $keyname,
'ContentType' => 'video/mp4',
]);
$request = $client->createPresignedRequest($command, $expires);
return (string) $request->getUri();
}
But, when I try to use the generated URL, I get the following error:
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided.
Check your key and signing method.</Message
I've created an upload and download service in php-symfony2. This is working fine. Now I want to delete the uploaded file. Any Example?
Note: No data storing to database tables.
Deleting One Object (Non-Versioned Bucket)
Create instance of S3 client using Aws\S3\S3Client class factory().
$s3 = S3Client::factory();
Execute the Aws\S3\S3Client::deleteObject() method with bucket name and a key name.
$result = $s3->deleteObject(array(
'Bucket' => $bucket,
'Key' => $keyname
));
If versioning is enabled DELETE MARKER Will be added. (References)
EXAMPLE
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
$s3 = S3Client::factory();
$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
$result = $s3->deleteObject(array(
'Bucket' => $bucket,
'Key' => $keyname
));
More References can be found here.
You can use deleteObject() method, refer the doc.
use Aws\S3\S3Client;
$s3 = S3Client::factory();
$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
$result = $s3->deleteObject(array(
'Bucket' => $bucket,
'Key' => $keyname
));
After lot of solution i tried i found the solution that works for me perfectly.
$s3 = Storage::disk('s3');
$s3->delete('filename');
You can use the aws s3 delete API method which delete the uploaded file. you can achieve it like below.
use Aws\S3\S3Client;
$s3 = new S3(awsAccessKey, awsSecretKey);
$s3->deleteObject("bucketname", filename);
This Code Worked For me.
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'your region',
'credentials' => [
'key' => '**AWS ACCESS KEY**',
'secret' => '**AWS SECRET ACCESS KEY**',
],
]);
try {
$result = $s3Client->deleteObject(array(
'Bucket' => '**YOUR BUCKET**',
'Key' => "videos/file.mp4"
));
return 1;
} catch (S3Exception $e) {
return $e->getMessage() . PHP_EOL;
}
Unfortunately, Arsalan's answer doesn't appear to work anymore. What worked for me, using access and secret keys was:
$this->s3 = new S3Client([
'driver' => 's3',
'version' => 'latest',
'region' => env('AWS_DEFAULT_REGION'),
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY')
]
]);
$this->s3->putObject([
'Bucket' => env('AWS_BUCKET'),
'Key' => env('AWS_SECRET_ACCESS_KEY'),
'Body' => $body,
'Key' => $key
]);
I see lots of tutorials even on Amazon to get this done. I follow it but for some reason it doesn't work.
I can do the other command below that works great, but deleting a bucket is not working, with no output for errors.
require 'vendor/autoload.php';
$key = 'file.txt'; // filename
$bucket = 'BUCKETNAME';
use Aws\S3\S3Client;
$client = S3Client::factory([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'KEY',
'secret' => 'SECRET'
]
]);
$result = $client->deleteObject(array(
'Bucket' => $bucket,
'Key' => $key
));
This works, but it isn't a delete command (GetObject) :
$cmd = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => 'file.txt'
]);
$request = $client->createPresignedRequest($cmd, '+20 minutes');
echo $presignedUrl = (string) $request->getUri();
To delete a bucket:
// Delete the bucket
$client->deleteBucket(array('Bucket' => $bucket));
You are missing the array.
Here:
http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#cleaning-up
I'm trying to upload a file from my Wordpress application to a S3 bucket by Ajax:
Somehow, I don't get an answer and the script fails with a 500 error when applying the 'putObject' method.
app/ajax.php
require_once 's3/start.php'
//wp_die(var_dump($s3)); Seems to be fine
$upload = $s3->putObject([
'Bucket' => $config['s3']['bucket'],
'Key' => 'video,
'Body' => fopen( $_FILES['file']['tmp_name'], 'r' ),
'ACL' => 'public-read',
]);
if ($upload) {
wp_die('Uploaded');
} else {
wp_die('Upload Error');
}
app/s3/start.php
use Aws\S3\S3Client;
require 'aws/aws-autoloader.php';
$config = require('config.php');
$s3 = new S3Client([
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret'],
'region' => $config['s3']['region'],
'version' => 'latest',
]);
app/s3/aws
Latest version of the official AWS SDK for PHP
SOLUTION
The credentials in app/start.php where not assigned correctly when initialising the $s3 object. That's how it must look like
$s3 = S3Client::factory([
'region' => $config['s3']['region'],
'version' => 'latest',
'credentials' => [
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret']
]
]);
If you upload a file, you should use SourceFile instead of Body.
Example code:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
More info from here - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html
I could not create Presigned Url using AWS SDK for PHP. My code is -
function connect()
{
// Instantiate the S3 class and point it at the desired host
date_default_timezone_set('GMT');
return S3Client::factory(array(
'region' => 'us-west-2',
'version' => 'latest',
'credentials' => [
'key' => $key,
'secret' => $secret
]
));
function getSignedS3URLForObject($fileName)
{
// GET CURRENT DATE
$milliseconds = round(microtime(true) * 1000);
$expiration = $milliseconds + (1000 * 60 * 60 * 24 * 30 * 2);
$s3 = self::connect();
$command = $s3->getCommand('GetObject', array(
'Bucket' => self::$customerBucket,
'Key' => $fileName,
'ContentType' => 'image/jpeg',
'Body' => '',
'ContentMD5' => false
));
$signedUrl = $command->createPresignedUrl($expiration);
echo urldecode($signedUrl);
return $signedUrl;
}
It gives me next error-
Fatal error: Call to undefined method
Aws\Command::createPresignedUrl() in
/Users/waverley_lv/WaverleySoftware/workspace/fox.php.auto/sites/default/behat-tests/util/S3Utility.php
on line 103
To force download any file on browser, you can use :
$command = $s3->getCommand('GetObject', array(
'Bucket' => 'bucketname',
'Key' => 'filename',
'ResponseContentType' => 'application/octet-stream',
'ResponseContentDisposition' => 'attachment'
));
// Create a signed URL from the command object that will last for
// 2 minutes from the current time
$response = $s3->createPresignedRequest($command, '+2 minutes');
$presignedUrl = (string)$response->getUri();
Using s3.0.0 v3 - I did the following to get this to work.
$command = $s3->getCommand('GetObject', array(
'Bucket' => $this->customerBucket,
'Key' => $fileName,
'ContentType' => 'image/png',
'ResponseContentDisposition' => 'attachment; filename="'.$fileName.'"'
));
$signedUrl = $s3->createPresignedRequest($command, "+1 week");