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
Related
I am uploading files from php to aws s3. I have successfully uploaded the file.
The url it is returning is => https://BUCKETNAME.s3.ap-south-1.amazonaws.com/images1740/1550830121572.jpg
The actual url is => https://s3.ap-south-1.amazonaws.com/BUCKETNAME/images1740/1550830121572.jpg
(bucket name is coming in starting instead at the end of url)
Because of this it is giving me error while loading images => "Specified Key not found"
$source = $source;
$bucket = 'xxxxxxxxxxxxxxxxx';
$keyname = 'images'.$usr_id."/".$name;
// for push
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'version' => 'latest',
'region' => 'ap-south-1'
)
);
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
));
// Print the URL to the object.
print_r($result);
return $result['ObjectURL'] . PHP_EOL;
// print_r($result);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
Set use_path_style_endpoint to true when initializing the S3 client to have it use the S3 path style endpoint by default when building the object URL. 1
Implementation details has the object URL to be in the path style if the bucket name makes a valid domain name otherwise it fallback to the S3 path style.
You want to keep the later behavior all the time.
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'use_path_style_endpoint' => true,
'version' => 'latest',
'region' => 'ap-south-1'
)
);
You can also do as below if you wanted to disable it one-time for the PutObject operation.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
'#use_path_style_endpoint' => true
));
Here I have the code of connection
$s3 = S3Client::factory(array(
'key' => 'xxxxxxxxx',
'secret' => '0xxxxxxxxxx',
'version' => 'latest',
'region' => 'us-west-2',
))
Here the bucket name $bucket = 'ariana-ios-storages'; and here the name of file $p = parse_url($images->name) which is the
name of file in amazon to delete;
Here is the code to delete the file
$delete = $s3->deleteObject([
'Bucket' => $bucket,
'Key' => $p['path']
]);
var_dump($delete);
exit();
The file path is generated like this;
It is not deleting the single file from the bucket so can any one help what i am missing
public function actionDeleteImages($id)
{
$exportImage= \common\models\ExportImages::findOne($id);
$base_url='base url to bucket';
$s3 = S3Client::factory(array(
'key' => 'xxxxxxx',
'secret' => '0pdfOx21xxxxxxxxxx',
'version' => 'latest',
'region' => 'us-west-2',
));
$bucket = 'name of bucket';
$key=urldecode(explode($base_url,$exportImage->name)[1]);// $images->name is Path from Db, base url is path to s3 server
$delete = $s3->deleteObject([
'Bucket' => 'ariana-ios-storages',
'Key' => $key
]);
$object_exist = $s3->doesObjectExist($bucket,$key);
if(!$object_exist){
return "succesfully deleted";
}else{
return "there is some problem while procesing the deletion";
}
}
}
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 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();