Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file size from Amazon AWS S3 Version 2?

I am forced to use Version 2 of AWS S3, because i cannot update PHP to 5.5 on this server in order to use Version 3.

I made this PHP script to download files from AWS, which works good:

//http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_createPresignedUrl
// Get a command object from the client and pass in any options
// available in the GetObject command (e.g. ResponseContentDisposition)
$command = $s3Client->getCommand('GetObject', array(
    'Bucket' => $bucket,
    'Key' => $objectKey,
    'ResponseContentDisposition' => 'attachment; filename="' . $originFilename . '"'
));

// Create a signed URL from the command object that will last for
// 10 minutes from the current time
$signedUrl = $command->createPresignedUrl('+1000 minutes');

$file = file_get_contents($signedUrl);

The problem is that i want to be sure that the file_get_contents() downloads the entire file and to detect and fix any error (like server going offline during a download, etc...), so i thought the following flow:

  1. I ask AWS the file size
  2. I download the file
  3. I check the size. If it's not equal i re-download the file

So, how to get file size from AWS? I found this, but it doesn't work for my version.

like image 204
Alberto Fontana Avatar asked Jan 14 '16 17:01

Alberto Fontana


People also ask

How do I find the total size of my AWS S3 storage bucket or folder?

Get the Size of a Folder in S3 # Open the AWS S3 console and click on your bucket's name. Optionally use the search input to filter by folder name. Click on the checkbox next to your folder's name. Click on the Actions button and select Calculate total size.


1 Answers

You can use the HEAD Object REST API to determine the size of the object stored on S3.

HEAD Object will return the meta-data associated with the stored S3 Object, including the size on disk of the object, within the Content-Length header.

http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_headObject

like image 196
Paddez Avatar answered Sep 28 '22 06:09

Paddez