如何使用Java上传文件夹到Google Cloud?

huangapple go评论141阅读模式
英文:

How to upload Folder in google cloud using java?

问题

以前我在AWS工作,现在在Google Cloud是新手,在AWS中有一种方法可以将目录/文件夹上传到存储桶中。我已经进行了一些研究,想要在Google Cloud存储桶中上传目录/文件夹,但是找不到方法。有人可以帮助我,如何在Google Cloud中使用Java实现这个目标。

英文:

Previously I was working in AWS and I am new in Google Cloud, in AWS there was a way to upload directories/folder to bucket. I have done bit of research for uploading directory/folder in Google Cloud bucket but couldn't find. Can someone help me, how to achieve this in Google Cloud using Java.

答案1

得分: 2

Google Cloud Storage客户端库(甚至API中)没有嵌入的函数来自动执行此操作。您必须递归上传所有文件,自行管理文件夹树的探索。

使用gcloud CLI,您可以使用命令[gsutil cp -r ...-r代表"递归",它执行完全相同的操作。

英文:

There is no embedded functions in the Google Cloud Storage client library (or even in the API) to perform this automatically. You have to recursively upload all your files, managing yourselves the folders tree exploration.

With the gcloud CLI, you can use the command gsutil cp -r .... The -r stands for "recursive" and it performs exactly the same operation.

答案2

得分: 1

以下是您提供的Java代码的中文翻译:

package com.example.app;

import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import java.io.File;

public class App {
    public static void main(String[] args) throws IOException {
        // 要上传的目录
        String dir = "/home/user/repo";

        // 获取父目录的名称
        String[] path = dir.split("/");
        String folder = path[path.length - 1];

        // 获取主目录中的文件
        File[] files = new File(dir).listFiles();

        // 定义您的项目ID和存储桶名称
        String bucket = "myawesomefolder";
        String projectId = "myawesomeprojectID";
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

        System.out.println("正在上传文件夹:" + folder);
        uploadFolder(files, folder, bucket, storage);
    }

    static void uploadFolder(File[] files, String folder, String bucket, Storage storage) throws IOException {
        for (File file : files) {
            if (!file.isHidden()) {
                // 如果是目录,则读取子目录中的文件
                if (file.isDirectory()) {
                    String[] lpath = file.getAbsolutePath().split("/");
                    String lfolder = lpath[lpath.length - 1];
                    String xfolder = folder + "/" + lfolder;
                    uploadFolder(file.listFiles(), xfolder, bucket, storage); // 再次调用相同的方法。

                } else {
                    // 将目录/子目录添加到文件名以创建文件结构
                    BlobId blobId = BlobId.of(bucket, folder + "/" + file.getName());

                    // 准备对象
                    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();

                    // 上传对象
                    storage.create(blobInfo, Files.readAllBytes(Paths.get(file.getAbsolutePath())));
                    System.out.println("已上传:gs://" + bucket + "/" + folder + "/" + file.getName());
                }
            }
        }
    }
}

希望这个翻译对您有所帮助。如果您有任何其他问题或需要进一步的帮助,请随时提问。

英文:

In Google Cloud Storage client library for java is not built-in the functionality to upload folders, but I crafted this java code to upload folders to GCS, I used Open JDK 8 and Debian

App.java

package com.example.app;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;
public class App {
public static void main(String[] args) throws IOException {
//directory that you want to upload
String dir = "/home/user/repo";
// get the name of the parent directory
String[] path = dir.split("/");
String folder = path[path.length - 1];
//get files in main directory
File[] files = new File(dir).listFiles();
// define your projectID & bucket name
String bucket = "myawesomefolder";
String projectId = "myawesomeprojectID";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
System.out.println("Uploading folder: " + folder);
uploadFolder(files, folder, bucket, storage);
}
static void uploadFolder(File[] files, String folder, String bucket, Storage storage) throws IOException {
for (File file : files) {
if (!file.isHidden()) {
// if it is a directory read the files within the subdirectory
if (file.isDirectory()) {
String[] lpath = file.getAbsolutePath().split("/");
String lfolder = lpath[lpath.length - 1];
String xfolder = folder + "/" + lfolder;
uploadFolder(file.listFiles(), xfolder, bucket, storage); // Calls same method again.
} else {
// add directory/subdirectory to the file name to create the file structure
BlobId blobId = BlobId.of(bucket, folder + "/" + file.getName());
//prepare object
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
// upload object
storage.create(blobInfo, Files.readAllBytes(Paths.get(file.getAbsolutePath())));
System.out.println("Uploaded: gs://" + bucket + "/" + folder + "/" + file.getName());
}
}
}
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>testGcs</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.111.2</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-nio</artifactId>
<version>0.121.2</version>
</dependency>
</dependencies>
</project>

huangapple
  • 本文由 发表于 2020年8月4日 14:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63241561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定