1

파일 크기 제한이있는 서버 측 업로드를 피하기 위해 클라이언트 측의 오디오 파일을 내 Google Cloud Storage 버킷에 직접 업로드하려고합니다.Google Cloud Storage에 업로드 할 때 403 SignatureDoesNotMatch를 얻으시겠습니까?

내 문제 : 업로드시 403 SignatureDoesNotMatch 오류가 발생합니다. 내가 서명 된 URL을 생성 한

<Error> 
<Code>SignatureDoesNotMatch</Code> 
<Message> The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method</Message> 
<StringToSign> 
PUT 

audio/mp3 
1511112552 
/bucketname/pathtofile/019%20-%20top%20cntndr%20V1.mp3 
</StringToSign> 
</Error> 

: 여기

는 응답에서 오류가 발생합니다. 서명 된 URL이 Google 문서 도구에서 발견 된적인 지침이 다음 내장되어

https://storage.googleapis.com/google-testbucket/testdata.txt?GoogleAccessId= 
[email protected]&Expires=1331155464&Signature=BCl 
z9e4UA2MRRDX62TPd8sNpUCxVsqUDG3YGPWvPcwN%2BmWBPqwgUYcOSszCPlgWREeF7oPGowkeKk 
7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9 
sFpqXsQI8IQi1493mw%3D 

여기 https://cloud.google.com/storage/docs/access-control/create-signed-urls-program

그러나,이 문제를 처리하는 부분이 URL을 체결 자바 스크립트 클라이언트 측이 문서에 매우 불분명하다 : 그것은 다음과 같습니다 .

다음은 서명 된 URL을 만들고 반환하는 Python 코드입니다.

GOOGLE_SERVICE_CREDENTIALS = 'google-service-credentials.json' 

def get_signed_url(request): 
    filename = request.GET.get('filename') 
    expiration = request.GET.get('expiration') 
    type = request.GET.get('type') 
    signed_url = CloudStorageSignedURL(
       method='PUT', 
       file_name=filename, 
       expiration_m=expiration, 
       content_type=type 
       ) 
    signed_url = signed_url.sign_url() 


    return JsonResponse({ 'signed_url': signed_url }) 






class CloudStorageSignedURL(object): 


    def __init__(self, method, file_name, expiration_m, content_type): 
     self.HTTP_method = method 
     self.content_type = 'content-type: ' + content_type 
     self.expiration = int(expiration_m) 
     self.file_name = file_name 


    def sign_url(self): 


     expiration_dt = datetime.utcnow() + timedelta(minutes=self.expiration) 
     expiration = int(time.mktime(expiration_dt.timetuple())) 
     bucket_path = '/' + settings.CLOUD_STORAGE_BUCKET + '/dev/tests/' + self.file_name 
     signature_string = self.HTTP_method + '\n' + '\n' + self.content_type + "\n" + str(expiration) + '\n' + bucket_path 
     print(signature_string) 
     creds = ServiceAccountCredentials.from_json_keyfile_name(GOOGLE_SERVICE_CREDENTIALS) 
     client_email = creds.service_account_email 
     signature = creds.sign_blob(signature_string)[1] 
     encoded_signature = base64.urlsafe_b64encode(signature).decode('utf-8') 
     base_url = settings.CLOUD_STORAGE_ROOT + 'dev/tests/' + self.file_name 


     return base_url + '?GoogleAccessId=' + client_email + '&Expires=' + str(expiration) + '&Signature=' + encoded_signature 

클라이언트 측 자바 스크립트 파일

import $ from 'jquery'; 
import axios from 'axios'; 



$("document").ready(() => { 
    console.log('window loaded'); 


    $("#id_audio_file").change(function() { 

    const file = this.files[0] 

    const url = window.location.href.replace('submit/', 'upload/'); 
    $.get(url + `?filename=${file.name}&expiration=10&type=${file.type}`, (data) => { 
     upload(data.signed_url, file); 
    }) 

    }); 
}); 



function upload(url, file) { 

    const config = { 
    headers: { 
     'Content-Type': file.type, 
    } 
    } 

    axios.put(url, file, config) 
    .then(function (res) { 
     console.log(res); 
    }) 
    .catch(function (err) { 
     console.log(err); 
    }); 
} 
내가 여기에 모든 기반을 커버처럼 정말 기분이

를 업로드,하지만 난 분명히 뭔가 분을 놓친 거지. 어떤 도움이라도 대단히 감사하겠습니다!

+0

'Expires = 1331155464'는 약 5 년 전에 만료 된 URL의 액세스가 만료되었음을 의미합니다. 그럼에도 불구하고 오류가 있었을 것이다 : ' ExpiredToken 제공된 토큰이 만료되었습니다.

요청이 만료되었습니다 : 타임 스탬프
'. [API 탐색기] (https://developers.google.com/apis-explorer/#search/signblob/m/iam/v1/iam.projects.serviceAccounts.signBlob)를 사용하여 다음과 같은 대안으로 서명을 생성하는 것이 좋습니다. 'sign_url', 테스트 용 – Tudormi

답변

0

먼저 사용중인 서비스 계정에 파일을 버킷에 업로드 할 수있는 적절한 권한이 있는지 확인하십시오 (IAM settings 또는 access control 통해).

이것이 설정되면 코드 문제입니다. 나는 대신에 수동으로이라는 서명 된 URL을 생성하여 접근법을 시도했으며, 동일한 오류로 나에게도 실패했다. 그러나 generate_signed_url 메서드를 사용하여 the Google Cloud python client library에서 signedURL을 통해 파일을 업로드 할 수있었습니다. 더 적은 이야기, 더 많은 코드 :

from google.oauth2 import service_account 
import base64 
import json 
from datetime import datetime, timedelta 
import time 
import requests 
from google.cloud import storage 

# client = storage.Client() 
# bucket = client.get_bucket('a-test-bucket') 
# blob = bucket.blob('/test/mpthreetest.mp3') 

GOOGLE_SERVICE_CREDENTIALS = 'signed_url_account.json' 
FILENAME = 'mpthreetest.mp3' 
EXPIRATION = 1511826970 # epoch time 
TYPE = 'audio/mp3' 

creds = service_account.Credentials.from_service_account_file(GOOGLE_SERVICE_CREDENTIALS) 
bucket = storage.Client().get_bucket('a-test-bucket') 
blob = bucket.blob('test/mpthreetest.mp3') 

signed_url = blob.generate_signed_url(method='PUT', expiration=EXPIRATION, content_type=TYPE, credentials=creds) 

print (signed_url) 

req = requests.put(signed_url, open(FILENAME), headers={'Content-Type': TYPE}) 
print(req.content) 
+0

이 접근 방식은 매력처럼 작동했습니다. – scottc11