首页 > 解决方案 > 谷歌存储文件下载

问题描述

伙计们,为什么要下载存储在 Google 存储桶中的可公开访问的对象?

https://storage.cloud.google.com/convertedexcelfiles/test.png

使用诸如wgetcurl... 之类的工具似乎会混淆文件。

$ wget https://storage.cloud.google.com/convertedexcelfiles/test.png

...
$ ls -all -h
56K Feb  2 01:32 test.png

值得信赖的人也是如此:

package main

import (
    "io"
    "net/http"
    "os"
)

func main() {

    fileUrl := "https://storage.cloud.google.com/convertedexcelfiles/test.png"

    if err := DownloadFile("test.png", fileUrl); err != nil {
        panic(err)
    }
}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {

    // Get the data
    resp, err := http.Get(url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    // Create the file
    out, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer out.Close()

    // Write the body to file
    _, err = io.Copy(out, resp.Body)
    return err
}

似乎可靠的唯一方法是使用gsutil或浏览到 gcp 控制台。想法?

可能是因为它返回 a302吗?

curl -I https://storage.cloud.google.com/convertedexcelfiles/test.png                                                                                                        ~/Downloads/transforms/tmp
HTTP/2 302
content-type: application/binary
location: https://accounts.google.com/ServiceLogin?service=cds&passive=1209600&continue=https://storage.cloud.google.com/convertedexcelfiles/test.png&followup=https://storage.cloud.google.com/convertedexcelfiles/test.png
content-length: 0
date: Sun, 02 Feb 2020 06:33:13 GMT
server: ESF
x-xss-protection: 0
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000

如果是这样的话,如何获取可靠的链接?

标签: gogoogle-cloud-platformgoogle-cloud-storage

解决方案


正如人们所提到的,该文件的内容是 html.... 仔细查看后,以下内容是显而易见的:

请勿使用此处框中的 URL: 不要使用

而是右键单击 Public : 利用

如您所见,这 2 个 URL 略有不同。

https://storage.cloud.google.com/convertedexcelfiles/test.png https://storage.googleapis.com/convertedexcelfiles/test.png

第二个工作......我有一种奇怪的感觉,其他人,不太懂技术的人会在这方面度过一段糟糕的时光。凌晨 3 点,这令人愤怒。我应该只是简单地整理文件......但谁会预料到它!在用户界面谷歌上做得很好......见鬼?


推荐阅读