GAE standard project returns java.nio.file.NoSuchFileException when uploading file to GCS

30 views
Skip to first unread message

전하정

unread,
Jul 20, 2020, 3:00:59 PM7/20/20
to Google App Engine
Hi. I am trying to make a website that allows users to upload their own files, and I figured that it would be nice to store the files to the GCS.

Therefore, I am trying to make an API that can upload a file from a local computer(paths like C:/user/..) to my GCS bucket.

The problem is that when I run the project locally(at localhost:8080), the API works well and uploads the file to my bucket.

However, when I deploy the project to Google App Engine and run it(at {my-project-id}.appspot.com, it returns java.nio.file.NoSuchFileException and fails to upload the file.

I've been stuck with this problem for almost a week, and I am wondering if it is not allowed for GAE to get access to user's file system. 

Here is the code that I used to upload the project, which is the same as the code from google doc about this topic.

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(uploadReqDto.getBucketName(), uploadReqDto.getUploadFileName());
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    storage.create(blobInfo, Files.readAllBytes(Paths.get(uploadReqDto.getLocalFileLocation())));

 uploadReqDto.getUploadFileName() and uploadReqDto.getLocalFileLocation() are the parameter of my POST API, which looks like

{
  "uploadFileName": "sooktubeTest1.txt",
  "localFileLocation": "C:/users/my/file/path"
}

I am almost sure that there is a problem with my localFileLocation variable, but I don't know how to fix it. It seems like GAE cannot get any file path. 
Is there any special way to upload user's local file to GCS when the project is deployed on GAE?

thanks. 

vladoi

unread,
Jul 21, 2020, 5:37:39 AM7/21/20
to Google App Engine
According to the official documentation [1] you have to: 

a. Create the application specific upload URL.
b. Use this URL as the action for the form you use to accept uploads.

Please check this thread for a similar use case [2].

-------------------------------
[1]. https://cloud.google.com/appengine/docs/standard/php/googlestorage/user_upload
Reply all
Reply to author
Forward
0 new messages