Как загрузить каталог в GCP Bucket, используя java - PullRequest
0 голосов
/ 10 апреля 2020

В настоящее время я пытаюсь загрузить каталог в мое ведро на GCP. Мне удалось заставить его загружать отдельные файлы просто отлично. Мой класс UploadObject выглядит следующим образом:

package main;

import com.google.auth.oauth2.GoogleCredentials;
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.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class UploadObject {

    public static void uploadObject(String bucketName, String objectName, String filePath) throws IOException {

        StorageOptions storageOptions = StorageOptions.newBuilder()
                .setProjectId("<project id>")
                .setCredentials(GoogleCredentials.fromStream(new
                        FileInputStream("<json key>"))).build();
        Storage storage = storageOptions.getService();
        BlobId blobId = BlobId.of(bucketName, objectName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
        storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));

        System.out.println("File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
    }

}

И метод в моем основном классе, который использует этот другой класс, выглядит следующим образом:

private void btnLoadEngineActionPerformed(ActionEvent arg0) throws IOException {
    if (chooser.files.isEmpty()) {
        throw new FileNotFoundException("No files were selected!");
    } else {
        UploadObject upload = new UploadObject();
        Archiver archiver = ArchiverFactory.createArchiver("tar", "gz");
        for (File f : chooser.files) {
            String fileName = f.getParent() + "\\" + f.getName().substring(0, f.getName().indexOf(".tar.gz"));
            File dest = new File(fileName);
            archiver.extract(f, dest);
            upload.uploadObject(bucketId, dest.getName(), dest.getPath() + "\\");
        }
    }
}

Этот метод извлекает из файла gzip и затем пытается загрузить содержимое в GCP. Тем не менее, я получаю эту ошибку в результате:

java.nio.file.AccessDeniedException: C:\...\Data\<directory>
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.Files.readAllBytes(Unknown Source)
    at main.UploadObject.uploadObject(UploadObject.java:25)
    at main.EngineGUI.btnLoadEngineActionPerformed(EngineGUI.java:151)
    at main.EngineGUI.access$2(EngineGUI.java:141)
    at main.EngineGUI$3.actionPerformed(EngineGUI.java:128)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Я не могу найти нигде в Интернете, где объясняется, как это сделать, поэтому я подумал, что я задам себе вопрос и посмотреть, есть ли у кого-нибудь идеи. Как я уже сказал, он загружает обычные файлы просто отлично, но когда я пытаюсь каталог, он выдает это исключение.

...