title: Upload a file to Google Cloud Storage (Java)
body: <p>Basically, the issue is in the title: I cannot figure out how to upload a file to Google Storage  </p>
  
  <p>I have checked this webpage:<br>
  <a href="https://cloud.google.com/storage/docs/uploading-objects" rel="noreferrer">https://cloud.google.com/storage/docs/uploading-objects</a></p>
  
  <p>These two lines are suggested for "uploading objects" in Java language tab:  </p>
  
  <pre><code>InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
  Blob blob = bucket.create(blobName, content, "text/plain");
  </code></pre>
  
  <p>The <code>bucket</code> object above can be received from <code>storage</code> object, and that's actually the root of the problem</p>
  
  <p>In all tutorials I have seen so far (particularly in this one: <a href="https://support.google.com/dfp_premium/answer/1733127?hl=en" rel="noreferrer">https://support.google.com/dfp_premium/answer/1733127?hl=en</a>), the <code>storage</code> belongs to <code>com.google.api.services.storage.Storage</code> instead of <code>com.google.cloud.storage.Storage</code>, and is instantiated as follows:  </p>
  
  <pre><code>        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  
          GoogleCredential credential = new GoogleCredential.Builder()
              .setTransport(httpTransport)
              .setJsonFactory(JacksonFactory.getDefaultInstance())
              .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
              .setServiceAccountScopes(
                  Collections.singleton(STORAGE_SCOPE))
              .setServiceAccountPrivateKeyFromP12File(p12File).build();
  
          storage = new com.google.api.services.storage.Storage.Builder(httpTransport,
              JacksonFactory.getDefaultInstance(), credential)
              .setApplicationName(PROJECT_NAME).build();
  </code></pre>
  
  <p>After creating a storage object in this way, we may obtain a bucket object as follows:  </p>
  
  <pre><code>    com.google.api.services.storage.Storage.Buckets.Get object = storage.buckets().get(BUCKET_NAME);        
      com.google.api.services.storage.model.Bucket bucket = object.execute();
  </code></pre>
  
  <p>But I can't use <code>com.google.api.services.storage.model.Bucket</code> for uploading files because it doesn't have <code>create()</code> method mentioned above. I need to use <code>com.google.cloud.storage.Bucket</code> instead.</p>
  
  <p>I have checked this stackoverflow question too: <a href="https://stackoverflow.com/questions/42893395/upload-image-to-google-cloud-storage-java">Upload image to Google Cloud Storage (Java)</a><br>
  and the examples in the provided links indeed use <code>com.google.cloud.storage.Storage</code> and <code>com.google.cloud.storage.Bucket</code> but they never show HOW to pass credentials and instantiate <code>storage</code> object similar to the code above.  </p>
  
  <p>Could someone provide a clear and comprehensive Java snippet showing how to upload a file to Google Cloud Storage, including all necessary credential instantiations of <code>storage</code> object?</p>
  
  <p>EDIT. In other words, I need to customize the instantiation of <code>com.google.cloud.storage.Storage</code> like above, instead of using this line: <code>Storage storage = StorageOptions.getDefaultInstance().getService();</code>  </p>
  
  <p>EDIT2. Finally I was able to upload the file after retrieving the bucket by the following code now:  </p>
  
  <pre><code>GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(KEY_JSN)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/devstorage.read_write"));
  
  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
  
  Bucket bucket = storage.get(BUCKET_NAME);
  </code></pre>
  
  <p>One single subtle point remaining for me, is that the following line:  </p>
  
  <pre><code>Page&lt;Bucket&gt; buckets = storage.list();
  </code></pre>
  
  <p>returns an empty list. Does it make sense to receive an empty bucket list while being able to receive a particular bucket meta by <code>get(bucket_name)</code> ?</p>