I need to upload my files inside specific directories that I created on my amazon s3 storage. I always uploaded the files on the "absolute path" of my bucket doing something like so:
$s3->putObject(array(
'Bucket' => $bucket,
'ContentType' => $mime,
'Key' => $localImage,
'ACL' => 'public-read',
'SourceFile' => $localImage,
'CacheControl' => 'max-age=172800',
"Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years")),
'Metadata' => array(
'profile' => $localImage,
),
));
How can I define where this file should be uploaded on a given directory?
You must include that information in the "Key" parameter. S3 isn't actually a filesystem, it's more like a big (hash table) associative array. The "Bucket" is the name of the hash table, and the "Key" is the key (e.g., $bucket[$key] = $content). So all path/directory information must be a part of the "Key".
$localImage = '/Users/jim/Photos/summer-vacation/DP00342654.jpg';
$s3->putObject(array(
'Bucket' => 'my-uniquely-named-bucket',
'SourceFile' => $localImage,
'Key' => 'photos/summer/' . basename($localImage)
));
thank you Jeremy Lindblom, this is my python example that worked for me.
import boto3
s3 = boto3.resource('s3')
data = open('/home/briansanchez/www/red-hat.jpg', 'rb')
s3.Bucket('briansanchez').put_object(Key='www/red-hat.jpg', Body=data)
Updated code according to the latest SDK of AWS:-
$result = $s3->putObject(array(
'Bucket' => 'bucket name of S3',
'Key' => 'pawan-trying',
'SourceFile' => 'local image path or document root image path ',
'ContentType' => 'image',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
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
));
I am using dependency injector to setup S3's credentials:
// AWS S3 for PDF
$container['s3_pdf'] = function ($c) {
// Only load credentials from environment variables.
$provider = CredentialProvider::env();
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'ap-southeast-2',
'credentials' => $provider
]);
return $s3;
};
Then whenever I want to upload something I'd do:
$result = $this->s3_pdf->putObject(array(
'Bucket' => 'reports.omitted.com',
'Key' => 'temptest1.pdf',
'SourceFile' => 'assets/temp.pdf',
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
I want to be able to upload to S3 from different functions in the code without having to write the bucket name everytime, am I able to have the s3_pdf container return a function that only takes sourcefile and runs some code to figure out the sourcefile & destination & uploads to S3?
I know that I can use a class that would contain this function I'm after and use an object of that class in the functions where I need S3 but I'd rather use the dependency container if there is a way to do so.
This is the simplest example of my suggested wrapper function possible:
class WhateverYourClassIs
{
function putObject( $key, $sourceFile )
{
return $this->s3_pdf->putObject(array(
'Bucket' => 'reports.omitted.com',
'Key' => $ke,
'SourceFile' => $sourceFile,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
}
}
Or with an array
class WhateverYourClassIs
{
function putObject( $overloadedConfig )
{
$baseConfig = array(
'Bucket' => 'reports.omitted.com',
'Key' => NULL,
'SourceFile' => NULL,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array()
);
return $this->s3_pdf->putObject( array_merge_recursive( $baseConfig, $overloadedConfig ) );
}
}
$this->putObject(array(
'Key' => 'temptest1.pdf',
'SourceFile' => assets/temp.pdf'
));
I am using the following code to upload image to the ECS server. Here I am first storing the image first to a temporary location and then uploading that to the server.
$result = $s3->putObject(array(
'Bucket' => $this->bucket,
'SourceFile' => $temp,
'Key' => $Destination_folder,
'ACL' => 'public-read',
'ContentType' => 'text/plain',
'Expires' => $expire
));
I want to remove the use of this temporary location. So is there any way to upload image directly using the image content only.
Try using 'Body' of s3 putObject for image content.
$result = $s3->putObject(array(
'Bucket' => $this->bucket,
'Key' => $Destination_folder,
'Body' => $image_content
));
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'm learning how to use Amazon for storing images and have gotten to the point I can upload a file with PHP. The problem is when I upload an image it shows as a blank image like it didn't upload. Here's what I've got:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'Body' => $filepath,
'ContentType' => 'image/jpeg',
'ACL' => 'public-read',
'StorageClass' => 'STANDARD',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
Here's the image it uploaded (which doesn't help much): https://motorcyclealbum.s3.amazonaws.com/test.jpg
Solved
So, 'Body' needed to be 'SourceFile' if uploading an image. I'm a dork.
So, 'Body' needed to be 'SourceFile' if uploading an image. I'm a dork.