简体   繁体   中英

Uploading Files to Google Cloud Storage via Google App Engine using Java: (No such file or directory)

I am developing a Spring-MAVEN JAVA project(mostly REST APIs for backend) using GAE. I am also using GCS(Google Cloud Storage) for storing some images and videos. The problem is that when I execute my files in a local server(tomcat 8.0), it works perfectly and uploads files to GCS as well.

However, when I deploy the files to GAE and execute, it returns java.io.FileNotFoundException (No such file or directory) error . I guess the GAE server cannot find files from my computer.

I've tried many things, but I still haven't found the right answer yet.

These are my codes related to the problem:

    @RestController
    @RequiredArgsConstructor
    public class GCSController {

    private final GCSService gcsService;

    @PostMapping("gcs/upload")
    public ResponseEntity localUploadToStorage(@RequestBody UploadReqDto uploadReqDto) throws IOException {
         BlobInfo fileFromGCS = gcsService.uploadFileToGCS(uploadReqDto);
         return ResponseEntity.ok(fileFromGCS.toString());
  }
 } 


    @Service
    @RequiredArgsConstructor
    public class GCSService {

     private final Storage storage;

     @SuppressWarnings("deprecation")
     public BlobInfo uploadFileToGCS(UploadReqDto uploadReqDto) throws IOException {


         BlobInfo blobInfo =storage.create(
                 BlobInfo.newBuilder(uploadReqDto.getBucketName(), uploadReqDto.getUploadFileName())
                         .setAcl(new ArrayList<>(Arrays.asList(Acl.of(Acl.User.ofAllAuthenticatedUsers(), Acl.Role.READER))))
                         .build(),
                 new FileInputStream(uploadReqDto.getLocalFileLocation()));

         return blobInfo;
     }

    }

UploadReqDto makes constructor, getter for bucketname, uploadFileName and localFilelocation.

This is the maven dependency that I've added for the API:

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-storage</artifactId>
<version>1.2.3.RELEASE</version>

You can follow this example to upload files from local storage to GAE standard java (click "code samples" tab and "Java"), you can find more information o n this doc

Take into consideration that writing to local disk in app engine standard is limited to /tmp

If you are using a file path of the likes C:/users/.. on code deployed App Engine Java will not be able to find it

I just came up with a solution so here is my answer to my own question:

Fixed it by using signed url for my Goolge Cloud Storage bucket. I was on this problem for a week and figured that there is no proper way to get to your own file path(like C:/users/) when you are running your project on GAE.

Guess this google doc(uploading objects to GCS is only helpful when your project is running locally. So the solution to my problem was to generate a signed URL for my bucket and access to the bucket by it. Here is the link that was helpful: how to create signed url for GCS

Thank you to all who answered this question anyways!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM