Last active
June 22, 2018 20:31
-
-
Save hkarakose/d8f9ff78f7deb692e5302e06e8612ca6 to your computer and use it in GitHub Desktop.
Display list of files/folders in a Google Cloud Storage(GSC) bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.cloud.Page; | |
import com.google.cloud.storage.Blob; | |
import com.google.cloud.storage.Storage; | |
import com.labsite.repository.StorageFactory; | |
import java.util.Iterator; | |
/** | |
* User: Halil Karakose | |
* Date: 31/01/17 | |
* Time: 21:31 | |
*/ | |
public class ListFilesAndFoldersInBucket { | |
private static final String BUCKET_NAME = "ENTER_YOUR_BUCKET_NAME_HERE"; | |
private static final String SERVICE_ACCOUNT_KEY = "ENTER_YOUR_SERVICE_ACCOUNT_KEY"; | |
private static final String project_id = "ENTER_YOUR_PROJECT_ID"; | |
public static void main(String[] args) { | |
list(""); | |
} | |
public static void list(String directory){ | |
Storage storage = storageService(); | |
Page<Blob> blobs = storage.list(BUCKET_NAME, Storage.BlobListOption.currentDirectory(), Storage.BlobListOption.prefix(directory)); | |
Iterable<Blob> blobIterator = blobs.iterateAll(); | |
blobIterator.forEach(blob -> { | |
if (blob.isDirectory()) { | |
list(blob.getName()); | |
} else { | |
System.out.println(blob.getName()); | |
} | |
}); | |
} | |
public static Storage storageService() { | |
ServiceAccountCredentials oauth2 = null; | |
try { | |
oauth2 = ServiceAccountCredentials.fromStream(new StringBufferInputStream(SERVICE_ACCOUNT_KEY)); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return StorageOptions.newBuilder() | |
.setCredentials(oauth2) | |
.setProjectId(project_id) | |
.build() | |
.getService(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.google.cloud</groupId> | |
<artifactId>google-cloud</artifactId> | |
<version>0.18.0-alpha</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I updated gist.
You may acquire required dependencies if you use
com.google.cloud:google-cloud:0.18.0-alpha