我目前正在开展一个项目,我们应该可以将文件上传到谷歌云存储。因此,我们创建了一个 Bucket,并将 Maven 依赖项添加到我的本地“普通”应用程序中:
<dependencies>
<dependency>
<groupId>com.google.appengine.tools</groupId>
<artifactId>appengine-gcs-client</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
然后我开始读取一个本地文件,并尝试将其推送到谷歌云存储中:
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 中搜索我发现了这个条目:未找到 API 包 'channel' 或调用 'CreateChannel()'
它说appengine.tools -> gcs-client
没有这样的 AppEngine 应用程序就无法使用。但是,有没有一种简单的方法可以将文件上传到 Google Cloud Storage,而无需强制使用 AppEngine 服务?