0

I am currently trying to upload a directory to my bucket on GCP. I managed to get it to upload individual files just fine. My UploadObject class is as follows:

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);
    }

}

And the method in my main class that utilizes this other class is as follows:

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() + "\\");
        }
    }
}

This method extracts from a gzip file and then tries to upload the contents to GCP. However, I get this error as a result:

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)

I cannot seem to find anywhere else online that explains how to do this so I figured I would ask myself and see if anyone has any ideas. Like I said it uploads regular files just fine but when I try a directory it throws this exception.

Soni Sol
  • 2,367
  • 3
  • 12
  • 23
Ian Lundberg
  • 1,813
  • 10
  • 31
  • 48
  • It it possible that the error is in the driver method, seems like the problem similar to this https://stackoverflow.com/questions/28670576/getting-java-nio-file-accessdeniedexception-when-trying-to-write-to-a-folder – irvifa Apr 11 '20 at 03:22
  • You may need to check if the directory exists. Have you tried uploading a directory (other than zipped one) ? Also you may try executing the program as an administrator. – Shafiq I Apr 12 '20 at 23:30

0 Answers0