【问题标题】:Upload image to Google Cloud Storage (Java)将图像上传到 Google Cloud Storage (Java)
【发布时间】:2017-08-11 03:05:22
【问题描述】:

我想开发一个可以将任何图片上传到 Google Cloud Storage 的 java 应用程序(用于 pc)。尽管我整个晚上都在寻找解决方案,但我不知道如何开始。有没有人有将图片上传到 Google Cloud Storage 的经验?有没有比 Google Cloud Storage 更好的替代品?

【问题讨论】:

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


【解决方案1】:

有一个可爱的 Java API 用于使用 Google Cloud,包括存储:http://googlecloudplatform.github.io/google-cloud-java/0.10.0/index.html

这是一个使用此库上传和下载文件的示例桌面应用程序:https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/StorageExample.java

Google Cloud Storage 包含大量文档、指南和教程。这是这一切的根源:https://cloud.google.com/storage/docs/

【讨论】:

  • 感谢您的快速回复。为了安装Java API,我必须先安装google cloud sdk,对吧?
  • 不,不相关。如果您使用的是 Maven 或其他东西,那只是一种依赖。无论如何,安装 SDK 是个好主意。您会发现 gcloud 命令在使用 Google Cloud 时非常有用。
  • 感谢您的建议。
【解决方案2】:
  @Override
  public String uploadImage(String fileName , String filePath,String fileType) throws IOException {
    Bucket bucket = getBucket("sample-bucket");
    InputStream inputStream = new FileInputStream(new File(filePath));
    Blob blob = bucket.create(fileName, inputStream, fileType);
    log.info("Blob Link:"+blob.getMediaLink());
    return blob.getMediaLink();
  }


  private Bucket getBucket(String bucketName) throws IOException {
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(CREDENTIALS_PATH))
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
    Bucket bucket = storage.get(bucketName);
    if (bucket == null) {
      throw new IOException("Bucket not found:"+bucketName);
    }
    return bucket;
  }

CREDENTIALS_PATH,设置服务账号时下载,确保用户有权限上传bucket中的图片。

【讨论】:

    【解决方案3】:

    您可以公开您的文件,然后 blob 的媒体链接可以在任何地方访问。

    https://cloud.google.com/storage/docs/access-control/making-data-public

    这是我的解决方案。

    try {
        FileInputStream serviceAccount =
                new FileInputStream("firebaseKey/serviceAccountKey.json");
    
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://xxxxx.firebaseio.com")
                .build();
        FirebaseApp fireApp = FirebaseApp.initializeApp(options);
    
        StorageClient storageClient = StorageClient.getInstance(fireApp);
        InputStream testFile = new FileInputStream(file.getPath());
        String blobString = "NEW_FOLDER/" + "FILE_NAME.jpg";
        Blob blob = storageClient.bucket("xxxxxx.appspot.com")
                        .create(blobString, testFile , Bucket.BlobWriteOption.userProject("xxxxxxx"));
        blob.getStorage().createAcl(blob.getBlobId(), Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
        System.out.println(blob.getMediaLink());
    } catch (Exception e){
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2013-06-27
      相关资源
      最近更新 更多