Amazon aws - s3 bucket not uploading image - It creates only the key - php

I am using Laravel 5.0 and using "aws/aws-sdk-php-laravel": "~2.0"
Here is my script to upload the image
$s3 = App::make('aws')->get('s3');
$s3->putObject(array(
'Bucket' => 'greenhoppingbucket',
'Key' => 'sups',
'Body' => Input::file('file'),
));
After the execution only the key is uploaded in the s3 bucket
i.e., sups is created bucket but not the image.
What is the mistake i am doing and how can i fix this

try this:
$s3 = App::make('aws')->get('s3');
$s3->putObject(array(
'Bucket' => 'greenhoppingbucket',
'Key' => 'sups',
'Body' => File::get((string)Input::file('file')),
));
dont forget to add use File;
when you do 'Body' => Input::file('file'), you bassiclly putting the temp path into the body instead of the content of the file.
the File::get is simply getting the contents of a file

Related

Blob upload to s3 with cropme.js

I really need some help with this. I have never used blob as a resource before but I need to use cropme.js and it only outputs the file uploaded as a blob or base64. Here is the github repo for it: https://github.com/shpontex/cropme. I have it working 100% on my side and it gives me the output but I do not know how to go from here. I have tried to upload the blob as a file to Amazon S3 but all it does is save the file to S3 without an extension and when you try to open it, it is a the blob link. Which is useless because the blob will be removed once the browser is cleared. Uploading the base64 route does the same. I just need some direction here please.
Here is the function in app.js from cropme that gives the result:
let img = document.getElementById('cropme-result')
this.position = this.cropme.position()
this.cropme.crop({
type: 'blob', <-- here you can set it to either 'blob' or 'base64'
width: 600
}).then(function(res) {
img.src = res
document.getElementById('croppedimage').src = img.src
})
},
Here is part of the code which uploads the file to S3:
try {
$imagecropped = $_POST['image'];
$key = 'photos/random';
// S3 details
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => [
'key' => 'MYKEY',
'secret' => 'MYSECRET',
],
]);
$result = $s3Client->putObject([
'Bucket' => 'uploads',
'ACL' => 'public-read',
'Key' => $key,
'Body' => $imagecropped,
]);

I cannot find uploaded archive to amazon s3 glacier using php

I have uploaded files to Amazon S3 Glacier using PHP as per code below. Am sure that the file is uploaded to my free Glacier S3 Account as I can print uploaded files details, archiveId etc.
Here is my problem: I cannot access the uploaded file in my Glacier S3 vault. My Vault is empty. Please where can I find my uploaded files. Is there something like permission needed on my own end. am using free account
Here is the code:
require 'vendor/autoload.php';
use Aws\Glacier\GlacierClient;
use Aws\Glacier\TreeHash;
$client = new GlacierClient([
'version' => 'latest',
'region' => 'my region here',
'credentials' => [
'key' => 'my key here',
'secret' => 'my secret here'
]
]);
$filename ='test.txt';
$result = $client->uploadArchive([
'accountId' => 'my accountid here',
'archiveDescription' => 'my first desc',
'body' => fopen($filename, 'r'),
'checksum' => '',
'contentSHA256' => '',
'sourceFile' => 'test.txt',
'vaultName' => 'my-vaultname-here',
]);
echo "success uploaded;
echo $archiveId = $result->get('archiveId');
The code above works fine as I stated earlier in the post. I finally read amazon glacier documentations and I found out the any uploaded files can show up within 1 to 48 hours depending on the archived region. The archieved has been showed. it seems to take us-east region to archive the inventory after 8-9 hours since uploads. Thanks

Uploading file to S3 using presigned URL in PHP

I am developing a Web Application using PHP. In my application, I need to upload the file to the AWS S3 bucket using Presigned URL. Now, I can read the private file from the S3 bucket using pre-signed like this.
$s3Client = new S3Client([
'version' => 'latest',
'region' => env('AWS_REGION', ''),
'credentials' => [
'key' => env('AWS_IAM_KEY', ''),
'secret' => env('AWS_IAM_SECRET', '')
]
]);
//GetObject
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => env('AWS_BUCKET',''),
'Key' => 'this-is-uploaded-using-presigned-url.png'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
//This is for reading the image. It is working.
$presignedUrl = (string) $request->getUri();
When I access the $presignedUrl from the browser, I can get the file from the s3. It is working. But now, I am uploading a file to S3. Not reading the file from s3. Normally, I can upload the file to the S3 like this.
$client->putObject(array(
'Bucket' => $bucket,
'Key' => 'data.txt',
'Body' => 'Hello!'
));
The above code is not using the pre-signed URL. But I need to upload the file using a pre-signed URL. How, can I upload the file using a pre-signed URL. For example, what I am thinking is something like this.
$client->putObject(array(
'presigned-url' => 'url'
'Bucket' => $bucket,
'Key' => 'data.txt',
'Body' => 'Hello!'
));
How can I upload?
It seems reasonable that you can create a pre-signed PutPobject command by running:
$cmd = $s3Client->getCommand('PutObject', [
'Bucket' => $bucket,
'Key' => $key
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes')->withMethod('PUT');
Then you might want to perform the PUT call from PHP using:
file_put_contents(
$request->getUri(),
'Hello!',
stream_context_create(['http' => [ 'method' => 'PUT' ]])
);
If you want to create a URL that a browser can submit, then you need to have the browser send the file as a form POST. This AWS documentation explains how to create a pre-signed POST request with the fields that you then need to put into an HTML form and display to the user: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-presigned-post.html
Also, this answer might be useful: https://stackoverflow.com/a/59644117/53538

The last step of a AWS EC2 to S3 file upload

I have this code :
require '/home/ubuntu/vendor/autoload.php';
$sharedConfig = [
'region' => 'us-west-2',
'version' => 'latest'
];
$sdk = new Aws\Sdk($sharedConfig);
$s3Client = $sdk->createS3();
$result = $s3Client->putObject([
'Bucket' => 'my-bucket',
'Key' => $_FILES["fileToUpload"]["name"],
'Body' => $_FILES["fileToUpload"]["tmp_name"]
]);
It works, basically. It sends a file to S3. But it apparently sends it badly since it always shows as a corrupted file... Can anyone tell me what I am doing wrong?
To be specific - the image I am uploading is a jpg image and when I try to look at it on the S3 instance, I am told that it "cannot be displayed because it contains errors"

SourceFile Amazon AWS S3

I'm trying to upload a file to my bucket. I am able to upload with Body but not SourceFile. Here's my method:
$pathToFile='/explicit/path/to/file.jpg'
// Upload an object by streaming the contents of a file
$result = $s3Client->putObject(array(
'Bucket' => $bucket,
'Key' => 'test.jpg',
'SourceFile' => $pathToFile,
'ACL' => 'public-read',
'ContentType' => 'image/jpeg'
));
but I get this error:
You must specify a non-null value for the Body or SourceFile parameters.
I've tried different types of files and keep getting this error.
The issue had to do with not giving a good path. Looks like file_exists() only checks locally, meaning that it had to be within localhost's index.
Change
'SourceFile' => $pathToFile,
to
'Body' => $pathToFile,

Categories