1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.nio.file.Path; 23 import java.nio.file.Paths; 24 25 /** File downloader to download file from google cloud storage (GCS). */ 26 public class GCSFileDownloader { 27 private static final long TIMEOUT = 10000; // 10s 28 private static final long RETRY_INTERVAL = 1000; // 1s 29 private static final int ATTETMPTS = 3; 30 31 /** 32 * Download a file from a gcs bucket file. 33 * 34 * @param bucketName gcs bucket name 35 * @param filename the filename 36 * @return {@link InputStream} with the file content. 37 */ downloadFile(String bucketName, String filename)38 public InputStream downloadFile(String bucketName, String filename) throws IOException { 39 GCSBucketUtil bucket = new GCSBucketUtil(bucketName); 40 bucket.setTimeoutMs(TIMEOUT); 41 bucket.setRetryInterval(RETRY_INTERVAL); 42 bucket.setAttempts(ATTETMPTS); 43 44 Path path = Paths.get(filename); 45 String contents = bucket.pullContents(path); 46 return new ByteArrayInputStream(contents.getBytes()); 47 } 48 } 49