reading a file from s3 bucket with laravel getting error - amazon-s3

Im trying to get file from s3 bucket using getObject
$s3 = AWS::createClient('s3');
$file = $s3->getObject(array(
'Bucket' => 'hotel4cast',
'Key' => $path->path,
'SaveAs' => public_path()
));
I'm getting below error
Error executing
"GetObject" on "https://s3.amazonaws.com/mybucket/filename.xlsx";
AWS HTTP error: Unable to open /var/www/html/laravel/public/ using mode r+: fopen(/var/www/html/laravel/public/):
ailed to open stream: Is a directory
if i take SaveAs out and dump $file i get object of data, body, stream all that stuff but not sure what to do with that.

I have figured out, there is bug in aws sdk,
i was able to get file to save by storing path in var before calling getObject
$r = fopen(public_path() . '/myfile.xlsx', 'wb');
$s3 = AWS::createClient('s3');
$file = $s3->getObject(array(
'Bucket' => 'bucketname',
'Key' => $path->path,
'SaveAs' => $r
));

can you tell me that what exactly these equals too ? So, I can guide you accordingly.
$path->path = ???
public_path() = ???
Edited
your method params should be like this, you just passing the saveAs path but attaching the key name, So, add the keyname with saveAs path, it will be downloaded.
$s3 = AWS::createClient('s3');
$file = $s3->getObject(array(
'Bucket' => 'hotel4cast',
'Key' => $path->path,
'SaveAs' => public_path()."/filename.xlsx"
));
here are the examples of code, which I am using for uploading file and coping file
for uploading
$result = $this->S3->putObject([
'ACL' => 'public-read-write',
'Bucket' => 'xyz', // REQUIRED
'Key' => 'file.xlsx', // REQUIRED
'SourceFile' => public_path()."/xlsx/file.xlsx",
]);
for Coping from one bucket to another
$copy = $this->S3->copyObject(array(
'ACL' => 'public-read-write',
'Bucket' => 'xyz', // REQUIRED
'Key' => 'file.xlsx', // REQUIRED
'CopySource' => 'mybucketname/xlsx/file.xlsx,
));
but your file which is exists in s3 bucket should have permission to read. other wise it will give you error to saveAs, copy etc
here are multiple permissions, you can see here
'ACL' => 'private|public-read|public-read-write|authenticated-read|aws-exec-read|bucket-owner-read|bucket-owner-full-control',

Related

How to use Wasabi (AmazonS3) in sabre/dav?

I am building a WebDAV server using sabre/dav, I want to create a WebDAV server file storage location in Wasabi which is compatible with AmazonS3, I did some research and found something that looks like AWS.php but I don't know how to use it. If anyone knows how to do this specifically, please respond.
What we tried:
・Download s3dav (https://github.com/audionamix/s3dav) and install the file.
・Server.php was written as follows
<?php
use Sabre\DAV;
use Aws\S3\S3Client;
// The autoloader
require 'vendor/autoload.php';
$raw_credentials = array(
'credentials' => array(
'key' => '<insert-access-key>',
'secret' => '<insert-secret-key>'
),
//'profile' => 'wasabi',
'endpoint' => 'https://s3.wasabisys.com',
'region' => 'us-east-1',
'version' => 'latest',
'use_path_style_endpoint' => true,
'use_path_style' => true,
'use_ssl' => true,
'port' => 443,
'hostname' => 's3.wasabisys.com',
'bucket' => '<bucket-name>',
);
// establish an S3 Client.
$s3 = S3Client::factory($raw_credentials);
// Now we're creating a whole bunch of objects
//$rootDirectory = new DAV\FS\Directory('public');
$rootDirectory = new DAV\FS\S3Directory("/",'<bucket-name>',$s3);
// The server object is responsible for making sense out of the WebDAV protocol
$server = new DAV\Server($rootDirectory);
// If your server is not on your webroot, make sure the following line has the
// correct information
$server->setBaseUri('/server.php/');
// The lock manager is reponsible for making sure users don't overwrite
// each others changes.
$lockBackend = new DAV\Locks\Backend\File('data/locks');
$lockPlugin = new DAV\Locks\Plugin($lockBackend);
$server->addPlugin($lockPlugin);
// This ensures that we get a pretty index in the browser, but it is
// optional.
$server->addPlugin(new DAV\Browser\Plugin());
// All we need to do now, is to fire up the server
$server->exec();
Result:
The file name list is displayed, but it is displayed as 0 bytes.
Uploading is working, but other operations are not working (file size is correct on Wasabi).
”4.4.0 Exception Cannot traverse an already closed generator" is displayed.

Access Custom S3 Metadata After Completing Multipart Upload

I'm wanting to access the custom metadata for an object uploaded via the S3 multipart upload after firing off the completeMultipartUpload method.
I initiate a multipart S3 upload with some added custom metadata like so:
$response = $this->client->createMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $key,
'ContentType' => $type,
'Expires' => 60,
'Metadata' => [
'file-guid' => $fileGuid,
],
]);
When I complete the multipart upload, I'm wanting to access the file-guid metadata and pass it along in my response.
$result = $this->client->completeMultipartUpload([
'Bucket' => $this->bucket,
'Key' => $key,
'UploadId' => $uploadId,
'MultipartUpload' => [
'Parts' => $parts,
],
]);
$fileGuid = $result['?'] // Couldn't find the metadata in the result.
return response()->json(['file-guid' => $fileGuid]);
I've checked the S3 object after it's been uploaded and it shows the custom metadata, but I don't see how to access it. I assumed it would be part of the completeMultipartUpload response, but I'm not seeing it.
Any help would be appreciated. Thanks!
I found a solution, but it involves an additional request. If anyone knows of a way to access the metadata without making another request, that would be better.
$headObject = $this->client->headObject([
'Bucket' => $this->bucket,
'Key' => $key,
]);

When i download file programmatically from s3 bucket, It gives me error It looks like we don't support this file format

When I download file from s3 bucket, I am using aws-sdk for that, it downloads the files but when i open that file it says It looks like we don't support this file format, here is my full code of it, can anyone please check my code and help me why image is doesn't open the image, my wholde code is in PHP, It looks like small error but doesn't working for me
$bucket = '*****';
$keyname = '1560346461616.jpg';
$s3 = new S3Client([
'version' => 'latest',
'region' => '******',
'credentials' => [
'key' => '******',
'secret' => '******',
],
]);
$result = $s3->getObject([
'Bucket' => $bucket,
'Key' => $keyname,
]);
header("Content-Type: {$result['ContentType']}");
header('Content-Disposition: attachment; filename='.$keyname);
echo $result['Body'];
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
Check the content type of your .jpg file stored in s3. Ensure the metadata for this object says image/jpeg.I don't think it is related to the object you downloaded,maybe the download file was corrupted.

Uploading a file to S3 in laravel

I am using larvel 4.2 and I have got these libraries added in my vendor folder. aws-sdk-php and aws-sdk-php-laravel.
When I try to upload a file in s3 bucket, it uploads a file of 45B only and the content of that file is a string specifying file location on file system. $file object is legit and it has been constructed using $file = Input::file($field->element_name);
$s3 = App::make('aws')->get('s3');
$result = $s3->putObject(array(
'Bucket' => 'phxdevapp',
'Key' => $file_new_name,
'Body' => $file
));
Any pointers ?

File not uploading to s3 bucket using yii2tech/file-storage extension

I'm trying to upload file to my amazon s3 bucket.
My config looks like this
'fileStorage' => [
'class' => 'yii2tech\filestorage\amazon\Storage',
'awsKey' => 'dota2',
'awsSecretKey' => 'dota2',
'buckets' => [
'webfiles' => [
'fileSubDirTemplate' => '{ext}/{^name}/{^^name}',
'region' => 'us_e1',
'acl' => 'public',
],
]
],
My upload function looks like this
$bucket = Yii::$app->fileStorage->getBucket('webfiles');
$bucket->copyFileIn('/var/www/html/vasttag/web/', 'samplefinal.mp4');
$result= var_dump($bucket->fileExists('samplefinal.mp4'));
echo $result;
I get result as false. Yes I have double checked my path and my filename. Its perfectly fine. Anyone know where Im going wrong?