简体   繁体   English

Django - React - 谷歌云存储 SigedURL 上传不起作用

[英]Django - React - Google Cloud Storage SigedURL upload is not working

I have used signedURL with jquery/ajax and Django for uploading to Google Cloud Storage previously successfully.我之前已成功使用带有 jquery/ajax 和 Django 的signedURL 上传到谷歌云存储。

However with the Django - React setup I have not been able to establish a successful.但是,使用 Django - React 设置我无法建立成功。 upload yet.上传呢。

export const UploadVideo = async (form_data, file, signedurl, asset_uuid) => {
    let resultState = { state: '', data: {} };

    let config = {
        method: 'POST',
        url: signedurl,
        headers: {
            'Content-Type': 'video/mp4',
            "Access-Control-Allow-Origin": "*",
            'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,PATCH,OPTIONS'
        },
        data: form_data
    };

    await axios(config).then(function (response) {
        console.log(JSON.stringify(response.data));
        resultState.state = 'success';
    }).catch(function (error) {
        resultState.state = 'error';
        resultState.data.message = error.message;
        window.toastr.error(error.message);

        console.log(error)
    })

    return resultState;
}

export const CreateAssets = async (data, key) => {
    let resultState = { state: '', data: {} };

    await axios.get(`https://origin/asset/create/?category=1&title=%22` + data[key].title + `%22&size=1`)
        .then(res => {
            resultState.state = 'success';
            resultState.data = res.data;
        }).catch((err) => {
            resultState.state = 'error';
            resultState.data['message'] = err.message;
        })
    return resultState;
}

The code for react js get singed url and calling signedurl with the file dialog is available. react js 获取 singed url 并使用文件对话框调用 signedurl 的代码可用。

What shall I do for a succesful signedurl file upload to Google Cloud Storage ?我应该怎么做才能成功将签名 URL 文件上传到 Google Cloud Storage?

The CORS settings for the storage bucket is as follows :存储桶的 CORS 设置如下:

# cat google-storage-cors.json                                       
[
    {
      "origin": ["https://www.videoo.io", "https://videoo.io", "http://localhost:8000", "localhost:3000", "http://localhost:3000", "https://storage.googleapis.com", "*"],
      "responseHeader": ["Content-Type","x-goog-resumable", "*"],
      "method": ["GET", "HEAD", "DELETE", "PUT", "POST", "OPTIONS"],
      "maxAgeSeconds": 3600
    }
]%

# gsutil cors set  google-storage-cors.json gs://production-my-storage

在此处输入图像描述

When I use the following curl command as suggested by @JohnHanley, the following is the output that I get :当我按照@JohnHanley 的建议使用以下 curl 命令时,以下是我得到的输出:

% curl -X PUT --upload-file /Users/utku/Desktop/cat.mp4 "https://storage.googleapis.com/production-videoo-storage/b47a4ffd40be41f08c255b3f308d36e1?Expires=1654980179&GoogleAccessId=videoo-348016%40appspot.gserviceaccount.com&Signature=QDnQh%2FI3%2BHJiPj5htwdPn7JqWZH%2FNaogM0ZlzJCl4BZVks6T5Qa1GMCCvmjJZINM01lpB9S%2FZMqamyNHisFz%2FB2EDxstJMHtAbpdw7E%2BYSXmGYP2lLpsCqXAhXsncbyKJFwkcD%2BkX3yZGHGsbLqnJtQ3lghhJxjrprPdhj1zGg%2FwrnKEa5g2YdxI2LW6KAQFtV8zICitWn%2BhdGxzJK1LnTekcv1%2F7zILlN9RbPwdEGBJWd2F3VIGCtyrZSuNqOW66ptQ2TT7uMbM5guDPPq86eom3eHMEVlY4E%2BLeYf4RMBwJQ50QhH0%2BA4Vevd2%2Bfza92acgJUJRhOQ5Gr5rZ6TnA%3D%3D"


<?xml version='1.0' encoding='UTF-8'?><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


1654980179
/production-videoo-storage/b47a4ffd40be41f08c255b3f308d36e1</StringToSign></Error>

So the signed url should be created with this function :所以签名的 url 应该用这个函数创建:

def get_signed_url_for_upload(path):
    blob = settings.STORAGE.bucket.blob(path)
    expiration_time = timezone.now() + timedelta(minutes=120)
    signed_url = blob.generate_signed_url(
        version="v4",
        # This URL is valid for 15 minutes
        expiration=timedelta(minutes=15),
        # Allow PUT requests using this URL.
        method="PUT",
        content_type="application/octet-stream",)
    return signed_url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Ruby作为后端将React上的图像上传到Google云端存储 - How to upload images on React to Google Cloud Storage with Ruby as backend Google Cloud Storage 上传图片的良好做法 - Google Cloud Storage upload images Good Practices 使用 Node、Express、Multer 响应单个文件上传并存储到 Google Cloud Storage - React single file upload with Node, Express, Multer and store to Google Cloud Storage 使用可恢复上传 url 在 React js 中上传文件在谷歌云存储中失败,抛出 CORS 策略错误 - Uploading file in react js with resumable upload url is failing in google cloud storage, throwing CORS policy error 从 Google Cloud Storage 提供 React 应用程序 - Serve a React app from Google Cloud Storage 使用 Firebase react-JS google-cloud-storage 上传大文件(10-300Mo)超时 - timeout on large file (10-300Mo) upload using Firebase react-JS google-cloud-storage Django 在谷歌云虚拟机上进行反应部署 - Django with react deployment on google cloud vm 更新托管在 Google Cloud Storage 上的 React 应用程序出现 404 错误 - Updating a React App hosted on Google Cloud Storage getting 404 errors 为什么我不能将 React 应用程序部署到 Google Cloud Storage? - Why can't I deploy a React app to Google Cloud Storage? 如何在 react.js 中使用 Google Cloud Storage? - How to use Google Cloud Storage with react.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM