如何使用Java将一个目录上传到GCP存储桶

huangapple 未分类评论52阅读模式
英文:

How to upload a directory to a GCP Bucket using java

问题

我目前正在尝试将一个目录上传到我的 Google Cloud Storage 存储桶中。我已经成功地将单个文件上传。我的 UploadObject 类如下所示:

  1. package main;
  2. import com.google.auth.oauth2.GoogleCredentials;
  3. import com.google.cloud.storage.BlobId;
  4. import com.google.cloud.storage.BlobInfo;
  5. import com.google.cloud.storage.Storage;
  6. import com.google.cloud.storage.StorageOptions;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.nio.file.Files;
  10. import java.nio.file.Paths;
  11. public class UploadObject {
  12. public static void uploadObject(String bucketName, String objectName, String filePath) throws IOException {
  13. StorageOptions storageOptions = StorageOptions.newBuilder()
  14. .setProjectId("<项目ID>")
  15. .setCredentials(GoogleCredentials.fromStream(new
  16. FileInputStream("<JSON密钥路径>"))).build();
  17. Storage storage = storageOptions.getService();
  18. BlobId blobId = BlobId.of(bucketName, objectName);
  19. BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
  20. storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
  21. System.out.println("文件 " + filePath + " 已上传到存储桶 " + bucketName + ",命名为 " + objectName);
  22. }
  23. }

而在我的主类中使用这个 UploadObject 类的方法如下所示:

  1. private void btnLoadEngineActionPerformed(ActionEvent arg0) throws IOException {
  2. if (chooser.files.isEmpty()) {
  3. throw new FileNotFoundException("未选择任何文件!");
  4. } else {
  5. UploadObject upload = new UploadObject();
  6. Archiver archiver = ArchiverFactory.createArchiver("tar", "gz");
  7. for (File f : chooser.files) {
  8. String fileName = f.getParent() + "\\" + f.getName().substring(0, f.getName().indexOf(".tar.gz"));
  9. File dest = new File(fileName);
  10. archiver.extract(f, dest);
  11. upload.uploadObject(bucketId, dest.getName(), dest.getPath() + "\\");
  12. }
  13. }
  14. }

这个方法从一个 gzip 文件中提取内容,然后尝试将其上传到 Google Cloud Storage。然而,我得到了以下错误:

  1. java.nio.file.AccessDeniedException: C:\...\Data\<目录>
  2. at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
  3. at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  4. at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  5. at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
  6. at java.nio.file.Files.newByteChannel(Unknown Source)
  7. at java.nio.file.Files.newByteChannel(Unknown Source)
  8. at java.nio.file.Files.readAllBytes(Unknown Source)
  9. at main.UploadObject.uploadObject(UploadObject.java:25)
  10. at main.EngineGUI.btnLoadEngineActionPerformed(EngineGUI.java:151)
  11. at main.EngineGUI.access$2(EngineGUI.java:141)
  12. at main.EngineGUI$3.actionPerformed(EngineGUI.java:128)
  13. ...

我似乎找不到其他在线资料解释如何处理这种情况,所以我决定亲自询问一下,看看是否有人有什么想法。就像我之前所说,它可以正常上传普通文件,但是当我尝试上传一个目录时,会抛出这个异常。

英文:

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:

  1. package main;
  2. import com.google.auth.oauth2.GoogleCredentials;
  3. import com.google.cloud.storage.BlobId;
  4. import com.google.cloud.storage.BlobInfo;
  5. import com.google.cloud.storage.Storage;
  6. import com.google.cloud.storage.StorageOptions;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.nio.file.Files;
  10. import java.nio.file.Paths;
  11. public class UploadObject {
  12. public static void uploadObject(String bucketName, String objectName, String filePath) throws IOException {
  13. StorageOptions storageOptions = StorageOptions.newBuilder()
  14. .setProjectId(&quot;&lt;project id&gt;&quot;)
  15. .setCredentials(GoogleCredentials.fromStream(new
  16. FileInputStream(&quot;&lt;json key&gt;&quot;))).build();
  17. Storage storage = storageOptions.getService();
  18. BlobId blobId = BlobId.of(bucketName, objectName);
  19. BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
  20. storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
  21. System.out.println(&quot;File &quot; + filePath + &quot; uploaded to bucket &quot; + bucketName + &quot; as &quot; + objectName);
  22. }
  23. }

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

  1. private void btnLoadEngineActionPerformed(ActionEvent arg0) throws IOException {
  2. if (chooser.files.isEmpty()) {
  3. throw new FileNotFoundException(&quot;No files were selected!&quot;);
  4. } else {
  5. UploadObject upload = new UploadObject();
  6. Archiver archiver = ArchiverFactory.createArchiver(&quot;tar&quot;, &quot;gz&quot;);
  7. for (File f : chooser.files) {
  8. String fileName = f.getParent() + &quot;\\&quot; + f.getName().substring(0, f.getName().indexOf(&quot;.tar.gz&quot;));
  9. File dest = new File(fileName);
  10. archiver.extract(f, dest);
  11. upload.uploadObject(bucketId, dest.getName(), dest.getPath() + &quot;\\&quot;);
  12. }
  13. }
  14. }

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

  1. java.nio.file.AccessDeniedException: C:\...\Data\&lt;directory&gt;
  2. at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
  3. at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  4. at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  5. at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
  6. at java.nio.file.Files.newByteChannel(Unknown Source)
  7. at java.nio.file.Files.newByteChannel(Unknown Source)
  8. at java.nio.file.Files.readAllBytes(Unknown Source)
  9. at main.UploadObject.uploadObject(UploadObject.java:25)
  10. at main.EngineGUI.btnLoadEngineActionPerformed(EngineGUI.java:151)
  11. at main.EngineGUI.access$2(EngineGUI.java:141)
  12. at main.EngineGUI$3.actionPerformed(EngineGUI.java:128)
  13. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  14. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  15. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  16. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  17. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  18. at java.awt.Component.processMouseEvent(Unknown Source)
  19. at javax.swing.JComponent.processMouseEvent(Unknown Source)
  20. at java.awt.Component.processEvent(Unknown Source)
  21. at java.awt.Container.processEvent(Unknown Source)
  22. at java.awt.Component.dispatchEventImpl(Unknown Source)
  23. at java.awt.Container.dispatchEventImpl(Unknown Source)
  24. at java.awt.Component.dispatchEvent(Unknown Source)
  25. at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  26. at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  27. at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  28. at java.awt.Container.dispatchEventImpl(Unknown Source)
  29. at java.awt.Window.dispatchEventImpl(Unknown Source)
  30. at java.awt.Component.dispatchEvent(Unknown Source)
  31. at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  32. at java.awt.EventQueue.access$500(Unknown Source)
  33. at java.awt.EventQueue$3.run(Unknown Source)
  34. at java.awt.EventQueue$3.run(Unknown Source)
  35. at java.security.AccessController.doPrivileged(Native Method)
  36. at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  37. at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  38. at java.awt.EventQueue$4.run(Unknown Source)
  39. at java.awt.EventQueue$4.run(Unknown Source)
  40. at java.security.AccessController.doPrivileged(Native Method)
  41. at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  42. at java.awt.EventQueue.dispatchEvent(Unknown Source)
  43. at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  44. at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  45. at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  46. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  47. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  48. 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.

huangapple
  • 本文由 发表于 2020年4月10日 20:41:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61140469.html
匿名

发表评论

匿名网友

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

确定