英文:
How to upload a directory to a GCP Bucket using java
问题
我目前正在尝试将一个目录上传到我的 Google Cloud Storage 存储桶中。我已经成功地将单个文件上传。我的 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("<项目ID>")
.setCredentials(GoogleCredentials.fromStream(new
FileInputStream("<JSON密钥路径>"))).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("文件 " + filePath + " 已上传到存储桶 " + bucketName + ",命名为 " + objectName);
}
}
而在我的主类中使用这个 UploadObject 类的方法如下所示:
private void btnLoadEngineActionPerformed(ActionEvent arg0) throws IOException {
if (chooser.files.isEmpty()) {
throw new FileNotFoundException("未选择任何文件!");
} 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 文件中提取内容,然后尝试将其上传到 Google Cloud Storage。然而,我得到了以下错误:
java.nio.file.AccessDeniedException: C:\...\Data\<目录>
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)
...
我似乎找不到其他在线资料解释如何处理这种情况,所以我决定亲自询问一下,看看是否有人有什么想法。就像我之前所说,它可以正常上传普通文件,但是当我尝试上传一个目录时,会抛出这个异常。
英文:
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.
专注分享java语言的经验与见解,让所有开发者获益!
评论