【发布时间】:2014-11-06 10:45:29
【问题描述】:
我目前正在开展一个项目,我们应该可以将文件上传到 Google Cloud Storage。因此,我们创建了一个 Bucket,并将 Maven 依赖项添加到我的本地“普通”应用程序中:
<dependencies>
<dependency>
<groupId>com.google.appengine.tools</groupId>
<artifactId>appengine-gcs-client</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
然后我开始读取本地文件并尝试将其推送到 Google Cloud Storage 中:
try {
final GcsService gcsService = GcsServiceFactory
.createGcsService();
File file = new File("/tmp/test.jpg");
FileInputStream fis = new FileInputStream(file);
GcsFilename fileName = new GcsFilename("test1213","test.jpg");
GcsOutputChannel outputChannel;
outputChannel = gcsService.createOrReplace(fileName, GcsFileOptions.getDefaultInstance());
copy(fis, Channels.newOutputStream(outputChannel));
} catch (IOException e) {
e.printStackTrace();
}
我的copy 方法如下所示:
private static final int BUFFER_SIZE = 2 * 1024 * 1024;
private static void copy(InputStream input, OutputStream output)
throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
} finally {
input.close();
output.close();
}
}
我从中得到的只是:
The API package 'file' or call 'Create()' was not found.
在谷歌搜索了很多之后,阅读文档甚至在 bing 中搜索我发现了这个条目:The API package 'channel' or call 'CreateChannel()' was not found
它说没有这样的 AppEngine 应用程序就无法使用appengine.tools -> gcs-client。但是,有没有一种简单的方法可以将文件上传到 Google Cloud Storage,而无需强制使用 AppEngine 服务?
【问题讨论】:
标签: java google-cloud-storage client-library