troubleshooting Question

PHP 7 and returning the status code from AWS upload to a bucket

Avatar of dogsareit
dogsareitFlag for United States of America asked on
PHPAWS
1 Comment1 Solution14 ViewsLast Modified:
I have been searching to find out how I can retrieve the numeric status code after I upload a file to a AWS S3 bucket. I can retrieve the results of the ObjectURL  but I need the numeric status  code. I know if it returns 200, all is well. If not, then I want to take a different route.
I have seen examples for large files, streaming, multipart etc.
But these are very small files and I can not see using the uploader object nor multipart examples for these files. 
Below is my code -
thank you in advance and any help appreciated.
Function RtnAWSUpload($wrkProdFileLocation,$wrkAWSFileLocation)
{
   
    $bucket = 'somebucket';
    $keyname = 'somekey';
    $secretkey = 'someprvkey'; // PRVKEY

   // echo '<BR><BR>AT AWS UPLOAD RTN';
             
    $s3 = new S3Client([
        'version' => 'latest',
        'region'  => 'us-east-2',
        'credentials' => array('key'=>$keyname,
                     'secret'=>$secretkey)
        ]);


    try {
        // UPLOAD FILE TO AWS BUCKET
      
         $result = $s3->putObject([
        'Bucket' => $bucket,
        'Key' => $wrkAWSFileLocation,
        'SourceFile' => $wrkProdFileLocation,
        'ACL'    => 'public-read'
        ]);
      
    // PRINT URL TO OBJECT
        //echo '<BR><BR>AWS FILE UPLOAD RESULT:  ' . $result['ObjectURL'] . PHP_EOL;
      
      $strAWSUploadResult = $result['ObjectURL']; // need to return the numeric status code instead of message)
      
       $_SESSION['NbrOfFilesUploaded'] = ($_SESSION['NbrOfFilesUploaded'] + 1);

        } catch (S3Exception $e) {
         echo $e->getAwsErrorCode() . PHP_EOL;
         echo $e->getMessage() . PHP_EOL;
    }

   return $strAWSUploadResult;
}


Open in new window

ASKER CERTIFIED SOLUTION
dogsareit
Owner

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros