Google云存储:java.lang.RuntimeException:服务器回复400,可能是错误请求

时间:2015-06-05 08:15:42

标签: google-app-engine google-cloud-storage

我创建了一个使用谷歌云存储客户端库的应用程序,允许用户将文件上传到云端。我创建了一个存储桶并添加了我的项目帐户

我已经部署了我的应用并试图选择文件将其上传到谷歌云,它给了我以下例外:

Caused by: com.google.appengine.tools.cloudstorage.NonRetriableException: java.lang.RuntimeException: Server replied with 400, probably bad request: Request: PUT https://storage.googleapis.com/gemsnps_edunext_1/sample.pdf?upload_id=AEnB2Ur6pjafrkUb_S2BUJKxNClkNfwvaVt7s6rDwnLOm5R6oew0ps0_ilZI1lqP82EChEVqktsj0qA5TcVaVdmfowSpz6AlVg
User-Agent: App Engine GCS Client
Content-Length: 52326
Content-MD5: SB0Bpju4CXK/jcmWTsjiyQ==
Content-Range: bytes 0-52325/52326

52326 bytes of content

Response: 400 with 117 bytes of content
Content-Type: application/xml; charset=UTF-8
Content-Length: 117
Vary: Origin
<?xml version='1.0' encoding='UTF-8'?><Error><Code>InvalidArgument</Code><Message>Invalid argument.</Message></Error>

    at com.google.appengine.tools.cloudstorage.RetryHelper.doRetry(RetryHelper.java:120)
    at com.google.appengine.tools.cloudstorage.RetryHelper.runWithRetries(RetryHelper.java:166)
    at com.google.appengine.tools.cloudstorage.RetryHelper.runWithRetries(RetryHelper.java:156)
    at com.google.appengine.tools.cloudstorage.GcsOutputChannelImpl.close(GcsOutputChannelImpl.java:198)
    at com.application.sis.plateform.gae.GaeFileService.saveFile(GaeFileService.java:106)
    at

代码

            String bucketName = "gemsnps_edunext_1";

            String contentType = item.getContentType();
            GcsFileOptions.Builder fileOptionBuilder = new GcsFileOptions.Builder().mimeType(contentType).acl("public-read-write");

            if (fileName.toLowerCase().endsWith(".pdf") || fileName.toLowerCase().endsWith(".xls")
                    || fileName.toLowerCase().endsWith(".xlsx") || fileName.toLowerCase().endsWith(".txt")
                    || fileName.toLowerCase().endsWith(".ppt") || fileName.toLowerCase().endsWith(".doc")
                    || fileName.toLowerCase().endsWith(".docs") || fileName.toLowerCase().endsWith(".zip")
                    || fileName.toLowerCase().endsWith(".jar")) {
                fileOptionBuilder.contentDisposition("attachment;filename=" + item.getName());
            }

            fileOptionBuilder.cacheControl("no-cache");
            GcsFileOptions gcsFileOptions = fileOptionBuilder.build();


            GcsFilename gcsFilename = new GcsFilename(bucketName, fileName);
            GcsOutputChannel writeChannel = gcsService.createOrReplace(gcsFilename, gcsFileOptions);

            System.err.println("File Writing");
            writeChannel.write(ByteBuffer.wrap(IOUtils.toByteArray(stream)));
            System.err.println("Writer Closing");
            // Now finalize
           writeChannel.close();

代码的最后一行出错。

1 个答案:

答案 0 :(得分:0)

我在尝试从谷歌应用引擎中将xlsx文件写入谷歌云存储时遇到了同样的问题。

我更改了GCSOptions参数...

GcsFileOptions.Builder fileOptionBuilder = new GcsFileOptions.Builder().mimeType(contentType).acl("project-private");

请注意为ACL传递的参数。 希望这会有所帮助。