S3::getHttpUploadPostParams PHP Method

getHttpUploadPostParams() public static method

Get upload POST parameters for form uploads
public static getHttpUploadPostParams ( string $bucket, string $uriPrefix = '', constant $acl = self::ACL_PRIVATE, integer $lifetime = 3600, integer $maxFileSize = 5242880, string $successRedirect = "201", array $amzHeaders = [], array $headers = [], boolean $flashVars = false ) : object
$bucket string Bucket name
$uriPrefix string Object URI prefix
$acl constant ACL constant
$lifetime integer Lifetime in seconds
$maxFileSize integer Maximum filesize in bytes (default 5MB)
$successRedirect string Redirect URL or 200 / 201 status code
$amzHeaders array Array of x-amz-meta-* headers
$headers array Array of request headers or content type as a string
$flashVars boolean Includes additional "Filename" variable posted by Flash
return object
    public static function getHttpUploadPostParams($bucket, $uriPrefix = '', $acl = self::ACL_PRIVATE, $lifetime = 3600, $maxFileSize = 5242880, $successRedirect = "201", $amzHeaders = array(), $headers = array(), $flashVars = false)
    {
        // Create policy object
        $policy = new stdClass();
        $policy->expiration = gmdate('Y-m-d\\TH:i:s\\Z', self::__getTime() + $lifetime);
        $policy->conditions = array();
        $obj = new stdClass();
        $obj->bucket = $bucket;
        array_push($policy->conditions, $obj);
        $obj = new stdClass();
        $obj->acl = $acl;
        array_push($policy->conditions, $obj);
        $obj = new stdClass();
        // 200 for non-redirect uploads
        if (is_numeric($successRedirect) && in_array((int) $successRedirect, array(200, 201))) {
            $obj->success_action_status = (string) $successRedirect;
        } else {
            // URL
            $obj->success_action_redirect = $successRedirect;
        }
        array_push($policy->conditions, $obj);
        if ($acl !== self::ACL_PUBLIC_READ) {
            array_push($policy->conditions, array('eq', '$acl', $acl));
        }
        array_push($policy->conditions, array('starts-with', '$key', $uriPrefix));
        if ($flashVars) {
            array_push($policy->conditions, array('starts-with', '$Filename', ''));
        }
        foreach (array_keys($headers) as $headerKey) {
            array_push($policy->conditions, array('starts-with', '$' . $headerKey, ''));
        }
        foreach ($amzHeaders as $headerKey => $headerVal) {
            $obj = new stdClass();
            $obj->{$headerKey} = (string) $headerVal;
            array_push($policy->conditions, $obj);
        }
        array_push($policy->conditions, array('content-length-range', 0, $maxFileSize));
        $policy = base64_encode(str_replace('\\/', '/', json_encode($policy)));
        // Create parameters
        $params = new stdClass();
        $params->AWSAccessKeyId = self::$__accessKey;
        $params->key = $uriPrefix . '${filename}';
        $params->acl = $acl;
        $params->policy = $policy;
        unset($policy);
        $params->signature = self::__getHash($params->policy);
        if (is_numeric($successRedirect) && in_array((int) $successRedirect, array(200, 201))) {
            $params->success_action_status = (string) $successRedirect;
        } else {
            $params->success_action_redirect = $successRedirect;
        }
        foreach ($headers as $headerKey => $headerVal) {
            $params->{$headerKey} = (string) $headerVal;
        }
        foreach ($amzHeaders as $headerKey => $headerVal) {
            $params->{$headerKey} = (string) $headerVal;
        }
        return $params;
    }

Usage Example

Example #1
0
 public static function generateUploadPOSTParams($item, Zotero_StorageFileInfo $info, $useItemContentType = false)
 {
     if (strlen($info->hash) != 32) {
         throw new Exception("Invalid MD5 hash '{$info->md5}'");
     }
     if (!$item->isAttachment()) {
         throw new Exception("Item {$item->id} is not an attachment");
     }
     $linkMode = $item->attachmentLinkMode;
     switch ($linkMode) {
         // TODO: get these constants from somewhere
         case 0:
         case 1:
             break;
         default:
             throw new Exception("Attachment with link mode {$linkMode} cannot be uploaded");
     }
     $lifetime = 3600;
     $path = self::getPathPrefix($info->hash, $info->zip);
     $contentMD5 = '';
     for ($i = 0; $i < strlen($info->hash); $i += 2) {
         $contentMD5 .= chr(hexdec(substr($info->hash, $i, 2)));
     }
     $contentMD5 = base64_encode($contentMD5);
     if ($useItemContentType) {
         if ($info->zip) {
             $contentType = "application/octet-stream";
         } else {
             $contentType = $item->attachmentMIMEType;
         }
     } else {
         $contentType = $info->contentType;
     }
     $metaHeaders = array();
     $requestHeaders = array('Content-Type' => $contentType, 'Content-MD5' => $contentMD5);
     self::requireLibrary();
     S3::setAuth(Z_CONFIG::$S3_ACCESS_KEY, Z_CONFIG::$S3_SECRET_KEY);
     $params = S3::getHttpUploadPostParams(Z_CONFIG::$S3_BUCKET, $path, S3::ACL_PRIVATE, $lifetime, $info->size + 262144, 201, $metaHeaders, $requestHeaders, $info->filename);
     return $params;
 }
All Usage Examples Of S3::getHttpUploadPostParams