파일 크기 제한이있는 서버 측 업로드를 피하기 위해 클라이언트 측의 오디오 파일을 내 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);
});
}
내가 여기에 모든 기반을 커버처럼 정말 기분이
를 업로드,하지만 난 분명히 뭔가 분을 놓친 거지. 어떤 도움이라도 대단히 감사하겠습니다!
'Expires = 1331155464'는 약 5 년 전에 만료 된 URL의 액세스가 만료되었음을 의미합니다. 그럼에도 불구하고 오류가 있었을 것이다 : ' 제공된 토큰이 만료되었습니다. 요청이 만료되었습니다 : 타임 스탬프 '. [API 탐색기] (https://developers.google.com/apis-explorer/#search/signblob/m/iam/v1/iam.projects.serviceAccounts.signBlob)를 사용하여 다음과 같은 대안으로 서명을 생성하는 것이 좋습니다. 'sign_url', 테스트 용 –
Tudormi
ExpiredToken