upload to google cloud storage signed url with javascript

前端 未结 2 2061
野的像风
野的像风 2021-02-13 12:53

With the following code I\'m able to upload to my publicly writable bucket in google cloud storage. (allUsers has write permission). However If the bucket isn\'t publicly writab

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 13:16

    I was implementing the same issue. The problem is with SignedURL. After correcting the signedurl the upload worked like a charm.

    As I was using php. Below is the code for generating signed urls.

    private function createSignedUrl($objectName, $bucketName, $key, $serviceEmailAddress, $method = 'GET', $duration = 600)
    {
        $expires = time() + $duration;
    
    // Line breaks are important!
    $toSign = (
        $method . "\n" .
        /* Content-MD5 */ "\n" .
        /* Content Type */ "\n" .
        $expires . "\n" .
        $objectName
    );
    $signature = urlencode(base64_encode(JWT::encode($toSign, $key, 'HS256')));
    return array(
        'expires' => $expires,
        'accessid' => $serviceEmailAddress,
        'signature' => $signature,
    );
    }
    

提交回复
热议问题