[英]How can I delete a bucket in Google Cloud Platform from java?
[英]Google Cloud - Can't download from bucket
我在使用Google存儲桶服務將文件下載到存儲桶時遇到一些問題。 我花了一些時間在互聯網上搜索,沒有答案。
這是代碼:
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;
由於HTTP 400是無效的sintax,因此您需要驗證參數“ stringUrl”,因為問題就在那兒。
如果您可以發布生成該變量的代碼,則將很有幫助。
如果您有權限下載文件或使用OAuth,請查看以下內容: https : //developers.google.com/console/help/new/#generatingoauth2
編輯:好的,看來您沒有使用OAuth進行下載身份驗證,因此您需要讓文件具有打開權限,看看示例:
但我建議使用OAuth 2.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.