title: Move file from Google Storage Bucket to Google Drive using Java while running on the Google App Engine Flex
body: <p>I am running an application on the Google App Engine Flex environment.  My goal is to allow file uploads from the application to a Google Drive.  Since the App Engine does not have a file system (or one that I really understand), Google's documentation says to upload to a Storage Bucket:</p>
  
  <p><a href="https://cloud.google.com/appengine/docs/flexible/java/using-cloud-storage" rel="nofollow noreferrer">https://cloud.google.com/appengine/docs/flexible/java/using-cloud-storage</a></p>
  
  <p>I have that code working great - uploading to the Storage Bucket.  The Google Drive code is also working great.</p>
  
  <p>My question is - how do I get the Storage Bucket file up to the Google Drive?</p>
  
  <p>I found a few posts, which suggest that I have to download the file from the Storage Bucket, which, from my understanding, would have to be saved to a file system and then picked up by the Google Drive code to upload to the Google Drive.</p>
  
  <p><a href="https://stackoverflow.com/questions/48122091/copy-file-from-google-drive-to-google-cloud-storage-within-google">copy file from Google Drive to Google Cloud Storage within Google</a></p>
  
  <p><a href="https://stackoverflow.com/questions/25141998/how-to-download-a-file-from-google-cloud-storage-with-java">How to download a file from Google Cloud Storage with Java?</a></p>
  
  <p>All I need to do is to set a <code>java.io.File</code> object from the Storage Bucket download.  I have the <code>blob.getMediaLink()</code> returned from the Google Storage code - is it possible to use that instead of downloading?</p>
  
  <p>Google Storage code:</p>
  
  <pre><code>public static String sendFileToBucket(InputStream fileStream, String fileName) throws IOException {
      Logger.info("GoogleStorage: sendFileToBucket: Starting...");
  
      GoogleCredential credential = null;
      String credentialsFileName = "";
      Storage storage = null;
      Blob blob = null;
      List&lt;Acl&gt; acls = null;
  
      // credential = authorize();
  
      // storage = StorageOptions.getDefaultInstance().getService();
  
      try {
          Logger.info("GoogleStorage: sendFileToBucket: Getting credentialsFileName path...");
          credentialsFileName = Configuration.root().getString("google.storage.credentials.file");
          Logger.info("GoogleStorage: sendFileToBucket: credentialsFileName = " + credentialsFileName);
  
          Logger.info("GoogleStorage: sendFileToBucket: Setting InputStream...");
          InputStream in = GoogleStorage.class.getClassLoader().getResourceAsStream(credentialsFileName);
          if (in == null) {
              Logger.info("GoogleStorage: sendFileToBucket: InputStream is null");
          }
          Logger.info("GoogleStorage: sendFileToBucket: InputStream set...");
  
          try {
              storage = StorageOptions.newBuilder().setCredentials(ServiceAccountCredentials.fromStream(in)).build()
                      .getService();
          } catch (StorageException se) {
              System.out.println("--- START ERROR WITH SETTING STORAGE OBJECT ---");
              se.printStackTrace();
              System.out.println("--- END ERROR WITH SETTING STORAGE OBJECT ---");
          }
  
          // Modify access list to allow all users with link to read file
          acls = new ArrayList&lt;&gt;();
          acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
  
          try {
              Logger.info("GoogleStorage: sendFileToBucket: Setting Blob object...");
              blob = storage.create(BlobInfo.newBuilder(BUCKET_NAME, fileName).setAcl(acls).build(), fileStream);
              Logger.info("GoogleStorage: sendFileToBucket: Blob Object set...");
          } catch (StorageException se) {
              System.out.println("--- START ERROR WITH SETTING BLOB OBJECT ---");
              se.printStackTrace();
              System.out.println("--- END ERROR WITH SETTING BLOB OBJECT ---");
          }
      } catch (IOException ex) {
          System.out.println("--- START ERROR SENDFILETOBUCKET ---");
          ex.printStackTrace();
          System.out.println("--- END ERROR SENDFILETOBUCKET ---");
      }
      Logger.info("GoogleStorage: sendFileToBucket: blob.getMediaLink() = " + blob.getMediaLink());
      return blob.getMediaLink();
  }
  </code></pre>
  
  <p>Google Drive code:</p>
  
  <pre><code>public static String uploadFile(java.io.File file, String folderIDToFind) throws IOException {
      String fileID = "";
      String fileName = "";
      try {
          Logger.info("GoogleDrive: uploadFile: Starting File Upload...");
          // Build a new authorized API client service.
          Drive service = getDriveService();
          Logger.info("GoogleDrive: uploadFile: Completed Drive Service...");
  
          // Set the folder...
          String folderID = Configuration.root().getString("google.drive.folderid");
          Logger.info("GoogleDrive: uploadFile: Folder ID = " + folderID);
  
          String folderIDToUse = getSubfolderID(service, folderID, folderIDToFind);
  
          String fullFilePath = file.getAbsolutePath();
          Logger.info("GoogleDrive: uploadFile: Full File Path: " + fullFilePath);
          File fileMetadata = new File();
  
          // Let's see what slashes exist to get the correct file name...
          if (fullFilePath.contains("/")) {
              fileName = StringControl.rightBack(fullFilePath, "/");
          } else {
              fileName = StringControl.rightBack(fullFilePath, "\\");
          }
          String fileContentType = getContentType(fileName);
          Logger.info("GoogleDrive: uploadFile: File Content Type: " + fileContentType);
          fileMetadata.setName(fileName);
          Logger.info("GoogleDrive: uploadFile: File Name = " + fileName);
  
          Logger.info("GoogleDrive: uploadFile: Setting the folder...");
          fileMetadata.setParents(Collections.singletonList(folderIDToUse));
          Logger.info("GoogleDrive: uploadFile: Folder set...");
  
          // Team Drive settings...
          fileMetadata.set("supportsTeamDrives", true);
  
          FileContent mediaContent = new FileContent(fileContentType, file);
  
          File fileToUpload = service.files().create(fileMetadata, mediaContent).setSupportsTeamDrives(true)
                  .setFields("id, parents").execute();
  
          fileID = fileToUpload.getId();
          Logger.info("GoogleDrive: uploadFile: File ID: " + fileID);
      } catch (Exception ex) {
          System.out.println(ex.toString());
          ex.printStackTrace();
      }
      Logger.info("GoogleDrive: uploadFile: Ending File Upload...");
      return fileID;
  }
  </code></pre>
  
  <p>In the post above, I see how to get the Storage Bucket file - but how to do I get that over to the <code>java.io.file</code> object:</p>
  
  <p><a href="https://stackoverflow.com/questions/25141998/how-to-download-a-file-from-google-cloud-storage-with-java/44205060#44205060">How to download a file from Google Cloud Storage with Java?</a></p>
  
  <p>Appreciate the help.</p>