我目前正在一个项目中工作,我们需要将文件上传到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.
在谷歌上进行了大量搜索,阅读文档,甚至在必应上搜索后,我找到了这篇文章:API包“channel”或调用“CreateChannel()”未找到。
它说没有办法使用
appengine.tools -> gcs-client
而无需使用AppEngine App。但是是否有一种简单的方法可以上传文件到Google Cloud Storage而不被迫使用AppEngine服务呢?