我将使用以下代码将CSV文件或PDF文件上传到谷歌云存储:
public static String uploadFile(String fileName, String fileLocation, final String bucketName) throws IOException {
DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
DateTime dt = DateTime.now(DateTimeZone.UTC);
String dtString = dt.toString(dtf);
fileName += dtString;
// The InputStream is closed by default, so we don't need to close it here
// Read InputStream into a ByteArrayOutputStream.
File file = new File(fileLocation);
InputStream is = new FileInputStream(file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] readBuf = new byte[4096];
while (is.available() > 0) {
int bytesRead = is.read(readBuf);
os.write(readBuf, 0, bytesRead);
}
is.close();
// Convert ByteArrayOutputStream into byte[]
BlobInfo blobInfo =
storage.create(
BlobInfo
.newBuilder(bucketName, fileName)
// Modify access list to allow all users with link to read file
.setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
.build(),
os.toByteArray());
// return the public download link
return blobInfo.getMediaLink();
}
当我下载上传的文件时,如果是CSV文件就可以了,但是当我下载PDF文件时,它总是显示空白页。而且在控制台本身也显示空白页。
不要使用available()
,因为它只是提供了一种防止阻塞的方法,试图读取超过OS文件缓冲区已经读取的内容。它可以在任何时候为0,等待后续物理读取。
while (true) {
int bytesRead = is.read(readBuf);
if (bytesRead < 0) {
break;
}
os.write(readBuf, 0, bytesRead);
}
或者干脆用:
Path file = Paths.get(fileLocation);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Files.copy(file, os);
我应该做什么才能成功上传图片?任何帮助都将不胜感激。 非常感谢。
我们一直在使用服务,在(AWS)中,我们意外删除了一个目录,因此我们认为它应该在中,但是经过查看,尽管处于打开状态,但它并不存在。
在谷歌云存储中,我在名为图像的根桶中有一个名为猫的桶。我正在使用google-api-ruby-Client gem上传文件。我可以将文件上传到根桶“图像”,但上传到“图像/猫”不起作用。我知道谷歌云存储中的存储桶没有斜杠的概念,所以我无法弄清楚如何指定嵌套存储桶的名称。 这给出了nil:NilClass的错误
追踪下面。 相关的Python片段: 最终触发(从ssl库): 溢出错误:字符串长度超过2147483647字节 我想我缺少一些特殊的配置选项? 这可能与这名1.5岁的年轻人有关,显然他还没有解决问题:https://github.com/googledatalab/datalab/issues/784. 谢谢你的帮助! 完整跟踪: [File”/usr/src/app/gcloud/downlo
我想按照官方文档中提供的示例将一个文件上传到Google云存储 然而,我得到了一个错误: 线程“main”com.google.cloud.storage.StorageException中的异常:获取服务帐户的访问令牌时出错:400个错误请求{“错误”:“无效的授予”、“错误描述”:“无效的JWT:令牌必须是短期令牌(60分钟)并且在合理的时间范围内。请检查JWT声明中的iat和exp值。”位于
我很难让GCS JSON API中的“简单上传”方法在Python中工作。文件(https://developers.google.com/storage/docs/json_api/v1/how-tos/upload#simple)使它看起来微不足道,但我无法通过授权。我不完全理解授权/身份验证/密钥/令牌,但我正在尝试。我一直试图上传的bucket允许完全读写(它们是公共的),我已经生成并尝试