Question

How to upload image files to spaces using the AWS SDK for PHP

Here is a test Script I put together, which works, in that it uploads a file to a DO space

My problem is, no matter what goes in the $mimeType Variable, when uploaded the mime type always comes out as application/octet-stream. rendering the file useless as an image.

I have searched for an answer to this, but it is non-existent on the internet. I would appreciate any assistance,

Thanks

define('ROOT', $_SERVER['DOCUMENT_ROOT']);
require ROOT.'/vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

                        

$s3Params=[
	'version' => 'latest',
	'region' => 'ams3',
	'endpoint' => 'https://ams3.digitaloceanspaces.com',
	'credentials' => [
    	'key'    => 'CRCC7MJT3CVY4E565OO2',
    	'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
		],
	 'debug' => true
	]
;


$s3 = new S3Client($s3Params);

// Add a file to a Space

$putParams = [   
	'Content-Type'		=> $mimeType,
	'Content-Length'	=> $fileSize,
	'Bucket'      		=> 'afors',
	'Key'        		=> $targetName, // this is the save as file in the space
    'Body'        		=> $targetFile, // and this is the file name on this server
    'ACL'        		=> 'public-read',
	 ] ;

print_r( $putParams);

try {
      $result = $s3->putObject($putParams);
 
          // $s3->waitUntil('ObjectExists', array('Bucket' => 'afors', 'Key'    => $targetName ));
 
           echo $result->toArray();
         } 
catch (S3Exception $e) {
	 echo 'there has been an exception<br>';
    print_r( $e);
}

echo "<br><br>At the end of the file";
Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

it’s only Laravel? How can i use with codeigniter?

after studying the AWS SDK it shows, that the body parameter can be one of 3 things :-

String, resource, or

So all we need to do, in php, is take the filename, and use ita s a stream, instead of just the file path.

So the Body parameter becomes ‘Body’ => fopen($targetFile,rb),

in fopen r means read only, and b means binary.

The working code is now.

define('ROOT', $_SERVER['DOCUMENT_ROOT']);
require ROOT.'/vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

                        

$s3Params=[
	'version' => 'latest',
	'region' => 'ams3',
	'endpoint' => 'https://ams3.digitaloceanspaces.com',
	'credentials' => [
    	'key'    => 'CRCC7MJT3CVY4E565OO2',
    	'secret' => 'IX8CoT8sXw6j4SvJeRT1D+vCQc9E0RX42l+EYaZ2/PA'
		],
	 'debug' => true
	]
;


$s3 = new S3Client($s3Params);

// Add a file to a Space





$putParams = [   
	'ContentLength'	=> $fileSize,
	'ContentType'		=> $mimeType,
	
	'Bucket'      		=> 'afors',
	'Key'        		=> $targetName, // this is the save as file in the space
    'Body'        		=> fopen($targetFile,rb), // and this is the file name on this server
    'ACL'        		=> 'public-read',
	 ] ;

print_r( $putParams);

try {
      $result = $s3->putObject($putParams);
 
          // $s3->waitUntil('ObjectExists', array('Bucket' => 'afors', 'Key'    => $targetName ));
 
           echo $result->toArray();
         } 
catch (S3Exception $e) {
	 echo 'there has been an exception<br>';
    print_r( $e);
}

echo "<br><br>At the end of the file";

It appears, that the SDK doesn’t use hyphens in the Parameters.

So using ContentType, and ContentLength, instead of Content-Type and Content-Length, has resolved the mime type issue, unfortunately now, what it’s uploading, is the actual String of the $fileName, and not the actual image itself, So it’s back to studying the SDK