简体   繁体   English

如何使用 java 在谷歌云中上传文件夹?

[英]How to upload Folder in google cloud using 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.以前我在 AWS 工作,我是 Google Cloud 的新手,在 AWS 中有一种方法可以将目录/文件夹上传到存储桶。 I have done bit of research for uploading directory/folder in Google Cloud bucket but couldn't find.我对在 Google Cloud 存储桶中上传目录/文件夹做了一些研究,但找不到。 Can someone help me, how to achieve this in Google Cloud using Java.有人可以帮助我,如何使用 Java 在谷歌云中实现这一点。

There is no embedded functions in the Google Cloud Storage client library (or even in the API) to perform this automatically. Google Cloud Storage 客户端库(甚至 API)中没有嵌入函数来自动执行此操作。 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... .通过 gcloud CLI,您可以使用命令gsutil cp -r... The -r stands for "recursive" and it performs exactly the same operation. -r代表“递归” ,它执行完全相同的操作。

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在 java 的谷歌云存储客户端库中没有内置上传文件夹的功能,但我制作了这个 java 代码来将文件夹上传到 GCS,我使用了 Open JDK 8 和 Debian

App.java申请.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 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java上传谷歌云存储中的文件 - How to upload a file in the google cloud storage using java 如何使用Java将图像/视频上传到Google Cloud Storage - How to Upload Image/Videos to Google Cloud Storage Using Java 如何使用 Java API 在谷歌云存储桶中创建一个空文件夹 - How to create an empty folder in google cloud bucket using Java API Google Cloud Storage使用Java上传文件 - Google Cloud Storage upload a file using java 如何使用Google App Engine(Java)创建剩余端点以将多部分数据上传到Google云存储 - How to create a rest endpoint using Google App Engine (Java) to upload multi part data to google cloud storage 如何使用Java API将超过32Mb的文件上传到谷歌云存储 - How to upload a file more that 32Mb to google cloud storage using Java API 如何使用 java 或节点 js 将本地文件自动上传到谷歌云存储桶 - How to Automate upload local file to google cloud bucket using java or node js 如何使用Java API在一个调用中将多个文件上传到Google Cloud Storage? - How to upload multiple files to Google Cloud Storage in a single call using Java API? 如何使用Java App Engine正确上传(映像)文件到Google云端存储? - How to properly upload (image) file to Google Cloud Storage using Java App Engine? 如何使用Ajax,Java,Spring Framework将文件从网页上传到Google Cloud Storage - How to upload a file from webpage to Google Cloud Storage using Ajax, Java, Spring Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM