谷歌存储文件下载

时间:2020-02-02 06:30:25

标签: go google-cloud-platform google-cloud-storage

伙计们, 为什么要下载存储在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控制台。有想法吗?

是否可能是因为其返回了302

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

在这种情况下,如何获得可靠的链接?

1 个答案:

答案 0 :(得分:4)

正如人们所提到的,文件的内容为html...。经过更加仔细的观察,显而易见:

请勿在此处使用框中的URL: dont use

但是右键单击Public: use

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

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

第二个作品...我感到奇怪的是,其他一些在技术上不太熟练的人会对此感到痛苦。凌晨3点,这真令人发指。我本来应该只是整理文件的……但是谁能想到呢!谷歌UI上的出色工作...该死吗?

相关问题