【问题标题】:Google Cloud - Can't download from bucket谷歌云 - 无法从存储桶下载
【发布时间】:2014-08-21 02:06:50
【问题描述】:

我在使用存储桶的谷歌服务将文件下载到存储桶时遇到了一些问题。我花了一些时间在互联网上搜索并没有答案。

代码如下:

public static void downloadFileFromBucket(String stringUrl, String pathParaDownload) throws IOException {  
        URL somefile = new URL(stringUrl); 

        ReadableByteChannel rbc = null;
        try {
            rbc = Channels.newChannel(somefile.openStream());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        FileOutputStream fos = new FileOutputStream(pathParaDownload);
        long start = System.currentTimeMillis();
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        long end = System.currentTimeMillis();
        System.out.println("Tempo de download: ");
        System.out.println(end-start);
        fos.close();
    } 

这是控制台日志:

java.io.IOException: Server returned HTTP response code: 400 for URL: http://storage.googleapis.com/bucket-bruno/?key=AIzaSyBTOqMtlFD7KUsK7p9ObGlMBOJuQRyMxuM
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
    at java.net.URL.openStream(URL.java:1037)
    at projetoIntegracao.projetoIntegracao.DownloadBucket.downloadFileFromBucket(DownloadBucket.java:41)
    at projetoIntegracao.projetoIntegracao.DownloadBucket.main(DownloadBucket.java:33)

已编辑:这是变量构造:

String stringUrl = "http://storage.googleapis.com/mybucketname/"+fileNameWithExtension+"?key="+apiKey;

【问题讨论】:

    标签: java bucket


    【解决方案1】:

    由于HTTP 400是无效的sintax,你需要验证你的参数“stringUrl”,因为问题在那边。

    如果您可以发布生成此变量的代码,那将会很有帮助。

    如果您对自己的存储桶具有下载 de 文件或使用 OAuth 的权限,也请查看:https://developers.google.com/console/help/new/#generatingoauth2

    编辑: 好的,看来您没有使用OAuth进行下载验证,所以您需要让文件具有打开权限,看看示例:

    但我建议使用 OAuth 2.0

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2020-02-15
      相关资源
      最近更新 更多