【发布时间】:2019-03-24 16:21:33
【问题描述】:
我正在尝试从 Google Cloud Storage 下载文件。我正在使用 Google 的 Github 代码 here on line 1042。我相信我的错误在于文件路径。路径变量用于说明文件下载到的位置及其新名称,对吗?请注意,我正在使用 JSP 按钮来启动该过程。为了让自己更容易解决,我用字符串而不是 HttpServletRequest 替换了字符串和路径变量。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// [START storage_download_file]
// The name of the bucket to access
String bucketName = "media";
// The name of the remote file to download
String srcFilename = "File.rtf";
// The path to which the file should be downloaded
Path destFilePath = Paths.get("/Volumes/Macintosh HD/Users/ab/Desktop/File.rtf");
// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();
// Get specific file from specified bucket
Blob blob = storage.get(BlobId.of(bucketName, srcFilename));
// Download file to specified path
blob.downloadTo(destFilePath);
// [END storage_download_file]
}
原因:java.nio.file.NoSuchFileException: /Volumes/Macintosh HD/Users/ab/Desktop/File.rtf
【问题讨论】:
-
此路径是否存在于您运行代码的机器上?检查路径和文件
-
代码在 GCP 的 App Engine 上运行。我对 Path 变量感到困惑。我了解该代码会将 File.rtf 从 GCS 下载到我的计算机。它会将下载的文件放置在具有新名称的指定路径中,在本例中为相同名称,该名称在路径末尾指定。路径最终为 /Users/ab/Desktop/File.rtf。不包括 /Volumes/Macintosh HD。我得到了同样的错误。
-
如果该代码在 Google App Engine 上运行,它将尝试在 Google App Engine 机器上写入,而不是在您的计算机上。要将其下载到您的计算机,您必须将下载的字节写入 HTTP 响应输出流。
标签: java google-cloud-platform nosuchfileexception