Unable to override content disposition header in s3

后端 未结 2 1332
走了就别回头了
走了就别回头了 2020-12-28 23:54

I\'m using the following php function to give temporary access to the public for a private file.

function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY,          


        
2条回答
  •  时光取名叫无心
    2020-12-29 00:20

    The problem with your function is that the header values should be encoded in the final hyperlink, but not for signing. The following function corrects that:

    function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY, $AWS_s3_secret_key, $expire_seconds, $save_as)
    {
        $expires = time()+$expire_seconds;
        // S3 Signed URL creation
        $headers = array(
            'response-content-disposition' => 'attachment; filename=' . $save_as,
        );
        $resource = str_replace(array('%2F', '%2B'), array('/', '+'), rawurlencode($resource));
    
        $string_to_sign = "GET\n\n\n$expires\n/$bucket/$resource";
        $final_url = "http://s3.amazonaws.com/$bucket/$resource?";
    
        $append_char = '?';
        foreach ($headers as $header => $value) {
            $final_url .= $header . '=' . urlencode($value) . '&';
            $string_to_sign .= $append_char . $header . '=' . $value;
            $append_char = '&';
        }
    
        $signature = urlencode(base64_encode(hash_hmac('sha1', $string_to_sign, $AWS_s3_secret_key, true)));
    
        return $final_url . "AWSAccessKeyId=$AWS_S3_KEY&Expires=$expires&Signature=$signature";
    }
    

提交回复
热议问题