【问题标题】:Uploading objects to google cloud storage bucket java将对象上传到谷歌云存储桶java
【发布时间】:2018-01-17 14:32:59
【问题描述】:

我想上传简单的图片文件到谷歌云存储 当上传到根存储桶时,上传很顺利,但是当我尝试将图像上传到存储桶内的文件夹时,它失败了

以下是我这样做的代码

 static Storage sStorage;
 public static void uploadFileToServer(Context context, String filePath) {
    Storage storage = getStorage(context);
    StorageObject object = new StorageObject();
    object.setBucket(BUCKET_NAME);
    File sdCard = Environment.getExternalStorageDirectory();
    File file = new File(sdCard + filePath);
    try {
        InputStream stream = new FileInputStream(file);
        String contentType = URLConnection.guessContentTypeFromStream(stream);
        InputStreamContent content = new InputStreamContent(contentType, stream);

        Storage.Objects.Insert insert = storage.objects().insert(BUCKET_NAME, null, content);
        insert.setName(file.getName());
        insert.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我尝试将存储桶名称设置为 [PARENT_BUCKET]/[CHILD_FOLDER] 但它不起作用

【问题讨论】:

    标签: java google-cloud-storage


    【解决方案1】:

    GCS 有一个扁平的命名空间,“文件夹”只是客户端支持的抽象(基本上,将对象名称中的“/”字符视为文件夹分隔符)。

    因此,要上传到文件夹,您应该只将存储桶名称放在请求的存储桶字段中,并将文件夹放在对象字段的开头。例如,要上传到存储桶“my-bucket”、文件夹“my-folder”和文件夹内文件“file”,您需要将存储桶名称设置为“my-bucket”,将对象名称设置为“my-folder/”文件”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2015-03-01
      • 2018-07-11
      • 2020-07-04
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多