Add Headers to Image Before Uploading to S3 Storage

I got a good library to upload images to S3 storage from undesigned. I’m currently use 0.5.1-dev version. The library has a function that responsible to set headers for images that want to be uploaded. The function name is putObject. The function also to be used to set amazon meta header (AmzHeader) like x-amz-meta-cache-control and x-amz-meta-expires.

I think the function has a line to set the amz header but miss a line to set the image headers like Cache-Control and Expires. Here is the original code of the function (focused to the header).

  1. public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD, $serverSideEncryption = self::SSE_NONE)
  2. {
  3. ...
  4.  
  5. // We need to post with Content-Length and Content-Type, MD5 is optional
  6. if ($rest->size >= 0 && ($rest->fp !== false || $rest->data !== false))
  7. {
  8. $rest->setHeader('Content-Type', $input['type']);
  9. if (isset($input['md5sum'])) $rest->setHeader('Content-MD5', $input['md5sum']);
  10.  
  11. $rest->setAmzHeader('x-amz-acl', $acl);
  12. foreach ($metaHeaders as $h => $v)
  13. $rest->setAmzHeader('x-amz-meta-'.$h, $v);
  14. $rest->getResponse();
  15. } else
  16. $rest->response->error = array('code' => 0, 'message' => 'Missing input parameters');
  17.  
  18. ...
  19. return true;
  20. }

The $metaHeaders array variable is supplied like this.

  1. $max_age = 315360000;//10 years
  2. $img_expire = strtotime('+10 years');
  3. $headers = array(
  4. 'Cache-Control'=>'max-age='.$max_age,
  5. 'Expires'=>gmdate('D, d M Y H:i:s T',$img_expire)
  6. );

The cache control and expires meta for the image are set to 10 years. From putObject function at line 13, there is a function setAmzHeader to set the amazon meta header but there is no function to set the cache control and expires meta header. To add those meta headers, I need to add a new line like this (see line 10 below).

  1. public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array(), $storageClass = self::STORAGE_CLASS_STANDARD, $serverSideEncryption = self::SSE_NONE)
  2. {
  3. ...
  4.  
  5. // We need to post with Content-Length and Content-Type, MD5 is optional
  6. if ($rest->size >= 0 && ($rest->fp !== false || $rest->data !== false))
  7. {
  8. $rest->setHeader('Content-Type', $input['type']);
  9. if (isset($input['md5sum'])) $rest->setHeader('Content-MD5', $input['md5sum']);
  10. foreach ($metaHeaders as $h => $v) $rest->setHeader($h, $v);
  11.  
  12. $rest->setAmzHeader('x-amz-acl', $acl);
  13. foreach ($metaHeaders as $h => $v)
  14. $rest->setAmzHeader('x-amz-meta-'.$h, $v);
  15. $rest->getResponse();
  16. } else
  17. $rest->response->error = array('code' => 0, 'message' => 'Missing input parameters');
  18.  
  19. ...
  20. return true;
  21. }

Loading