How to use download method of com.qaprosoft.amazon.AmazonS3Manager class

Best Carina code snippet using com.qaprosoft.amazon.AmazonS3Manager.download

Source:AbstractTest.java Github

copy

Full Screen

...502 * Method to update MOBILE_APP path in case if apk is located in s3 bucket.503 */504 private void updateS3AppPath() {505 Pattern S3_BUCKET_PATTERN = Pattern.compile("s3:\\/\\/([a-zA-Z-0-9][^\\/]*)\\/(.*)");506 // get app path to be sure that we need(do not need) to download app507 // from s3 bucket508 String mobileAppPath = Configuration.get(Parameter.MOBILE_APP);509 Matcher matcher = S3_BUCKET_PATTERN.matcher(mobileAppPath);510 LOGGER.info("Analyzing if mobile_app is located on S3...");511 if (matcher.find()) {512 LOGGER.info("app artifact is located on s3...");513 String bucketName = matcher.group(1);514 String key = matcher.group(2);515 Pattern pattern = Pattern.compile(key);516 // analyze if we have any pattern inside mobile_app to make extra517 // search in AWS518 int position = key.indexOf(".*");519 if (position > 0) {520 // /android/develop/dfgdfg.*/Mapmyrun.apk521 int slashPosition = key.substring(0, position).lastIndexOf("/");522 if (slashPosition > 0) {523 key = key.substring(0, slashPosition);524 S3ObjectSummary lastBuild = AmazonS3Manager.getInstance().getLatestBuildArtifact(bucketName, key,525 pattern);526 key = lastBuild.getKey();527 }528 }529 S3Object objBuild = AmazonS3Manager.getInstance().get(bucketName, key);530 String s3LocalStorage = Configuration.get(Parameter.S3_LOCAL_STORAGE);531 String fileName = s3LocalStorage + "/" + StringUtils.substringAfterLast(objBuild.getKey(), "/");532 File file = new File(fileName);533 // verify maybe requested artifact with the same size was already534 // download535 if (file.exists() && file.length() == objBuild.getObjectMetadata().getContentLength()) {536 LOGGER.info("build artifact with the same size already downloaded: " + file.getAbsolutePath());537 } else {538 LOGGER.info(String.format("Following data was extracted: bucket: %s, key: %s, local file: %s", bucketName, key,539 file.getAbsolutePath()));540 AmazonS3Manager.getInstance().download(bucketName, key, new File(fileName));541 }542 R.CONFIG.put(Parameter.MOBILE_APP.getKey(), file.getAbsolutePath());543 LOGGER.info("Updated mobile_app: " + Configuration.get(Parameter.MOBILE_APP));544 }545 }546 protected void setBug(String id) {547 String test = TestNamingUtil.getTestNameByThread();548 TestNamingUtil.associateBug(test, id);549 }550 protected void skipExecution(String message) {551 throw new SkipException(SpecialKeywords.SKIP_EXECUTION + ": " + message);552 }553 // --------------------------------------------------------------------------554 // Web Drivers...

Full Screen

Full Screen

Source:AmazonS3Manager.java Github

copy

Full Screen

...182 }183 try {184 LOGGER.info("Finding an s3object...");185 // TODO investigate possibility to add percentage of completed186 // downloading187 S3Object s3object = s3client.getObject(new GetObjectRequest(bucket,188 key));189 LOGGER.info("Content-Type: "190 + s3object.getObjectMetadata().getContentType());191 return s3object;192 /*193 * GetObjectRequest rangeObjectRequest = new GetObjectRequest(194 * bucketName, key); rangeObjectRequest.setRange(0, 10); S3Object195 * objectPortion = s3client.getObject(rangeObjectRequest);196 */197 } catch (AmazonServiceException ase) {198 LOGGER.error("Caught an AmazonServiceException, which "199 + "means your request made it "200 + "to Amazon S3, but was rejected with an error response for some reason.\n"201 + "Error Message: " + ase.getMessage() + "\n"202 + "HTTP Status Code: " + ase.getStatusCode() + "\n"203 + "AWS Error Code: " + ase.getErrorCode() + "\n"204 + "Error Type: " + ase.getErrorType() + "\n"205 + "Request ID: " + ase.getRequestId());206 } catch (AmazonClientException ace) {207 LOGGER.error("Caught an AmazonClientException, which "208 + "means the client encountered "209 + "an internal error while trying to "210 + "communicate with S3, "211 + "such as not being able to access the network.\n"212 + "Error Message: " + ace.getMessage());213 }214 // TODO investigate pros and cons returning null215 throw new RuntimeException("Unable to download '" + key216 + "' from Amazon S3 bucket '" + bucket + "'");217 }218 /**219 * Delete file from Amazon S3 storage.220 * 221 * @param bucket222 * - S3 Bucket name.223 * @param key224 * - S3 storage path. Example:225 * DEMO/TestSuiteName/TestMethodName/file.txt226 */227 public void delete(String bucket, String key) {228 if (key == null) {229 throw new RuntimeException("Key is null!");230 }231 if (key.isEmpty()) {232 throw new RuntimeException("Key is empty!");233 }234 try {235 s3client.deleteObject(new DeleteObjectRequest(bucket, key));236 } catch (AmazonServiceException ase) {237 LOGGER.error("Caught an AmazonServiceException, which "238 + "means your request made it "239 + "to Amazon S3, but was rejected with an error response for some reason.\n"240 + "Error Message: " + ase.getMessage() + "\n"241 + "HTTP Status Code: " + ase.getStatusCode() + "\n"242 + "AWS Error Code: " + ase.getErrorCode() + "\n"243 + "Error Type: " + ase.getErrorType() + "\n"244 + "Request ID: " + ase.getRequestId());245 } catch (AmazonClientException ace) {246 LOGGER.error("Caught an AmazonClientException.\n"247 + "Error Message: " + ace.getMessage());248 }249 }250 /**251 * Get latest build artifact from Amazon S3 storage as S3Object.252 * 253 * @param bucket254 * - S3 Bucket name.255 * @param key256 * - S3 storage path to your project. Example:257 * android/MyProject258 * @param pattern259 * - pattern to find single build artifact Example:260 * .*prod-google-release.*261 * @return S3ObjectSummary262 */263 public S3ObjectSummary getLatestBuildArtifact(String bucket, String key, Pattern pattern) {264 if (pattern == null) {265 throw new RuntimeException("pattern is null!");266 }267 S3ObjectSummary latestBuild = null;268 ObjectListing objBuilds = s3client.listObjects(bucket, key);269 int i = 0;270 int limit = 100;271 // by default S3 return only 1000 objects summary so need while cycle here272 do {273 LOGGER.info("looking for s3 artifact using iteration #" + i);274 for (S3ObjectSummary obj : objBuilds.getObjectSummaries()) {275 LOGGER.debug("Existing S3 artifact: " + obj.getKey());276 Matcher matcher = pattern.matcher(obj.getKey());277 if (matcher.find()) {278 if (latestBuild == null) {279 latestBuild = obj;280 }281 if (obj.getLastModified().after(latestBuild.getLastModified())) {282 latestBuild = obj;283 }284 }285 }286 objBuilds = s3client.listNextBatchOfObjects(objBuilds);287 } while (objBuilds.isTruncated() && ++i < limit);288 if (latestBuild == null) {289 LOGGER.error("Unable to find S3 build artifact by pattern: " + pattern);290 } else {291 LOGGER.info("latest artifact: " + latestBuild.getKey());292 }293 return latestBuild;294 }295 /**296 * Method to download file from s3 to local file system297 * 298 * @param bucketName AWS S3 bucket name299 * @param key (example: android/apkFolder/ApkName.apk)300 * @param file (local file name)301 */302 public void download(final String bucketName, final String key, final File file) {303 download(bucketName, key, file, 10);304 }305 /**306 * Method to download file from s3 to local file system307 * 308 * @param bucketName AWS S3 bucket name309 * @param key (example: android/apkFolder/ApkName.apk)310 * @param file (local file name)311 * @param pollingInterval (polling interval in sec for S3 download status determination)312 */313 public void download(final String bucketName, final String key, final File file, long pollingInterval) {314 LOGGER.info("App will be downloaded from s3.");315 LOGGER.info(String.format("[Bucket name: %s] [Key: %s] [File: %s]", bucketName, key, file.getAbsolutePath()));316 DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();317 TransferManager tx = new TransferManager(318 credentialProviderChain.getCredentials());319 Download appDownload = tx.download(bucketName, key, file);320 try {321 LOGGER.info("Transfer: " + appDownload.getDescription());322 LOGGER.info(" State: " + appDownload.getState());323 LOGGER.info(" Progress: ");324 // You can poll your transfer's status to check its progress325 while (!appDownload.isDone()) {326 LOGGER.info(" transferred: " + (int) (appDownload.getProgress().getPercentTransferred() + 0.5) + "%");327 CommonUtils.pause(pollingInterval);328 }329 LOGGER.info(" State: " + appDownload.getState());330 // appDownload.waitForCompletion();331 } catch (AmazonClientException e) {332 throw new RuntimeException("File wasn't downloaded from s3. See log: ".concat(e.getMessage()));333 }334 // tx.shutdownNow();335 }336 /**337 * Method to generate pre-signed object URL to s3 object338 * 339 * @param bucketName AWS S3 bucket name340 * @param key (example: android/apkFolder/ApkName.apk)341 * @param ms espiration time in ms, i.e. 1 hour is 1000*60*60342 * @return url String pre-signed URL343 */344 public URL generatePreSignUrl(final String bucketName, final String key, long ms) {345 java.util.Date expiration = new java.util.Date();346 long msec = expiration.getTime();...

Full Screen

Full Screen

download

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.amazon;2import java.io.File;3import java.io.IOException;4import com.amazonaws.AmazonServiceException;5import com.amazonaws.SdkClientException;6import com.amazonaws.auth.AWSCredentials;7import com.amazonaws.auth.AWSStaticCredentialsProvider;8import com.amazonaws.auth.BasicAWSCredentials;9import com.amazonaws.services.s3.AmazonS3;10import com.amazonaws.services.s3.AmazonS3ClientBuilder;11import com.amazonaws.services.s3.model.GetObjectRequest;12import com.amazonaws.services.s3.model.S3Object;13import com.amazonaws.services.s3.model.S3ObjectInputStream;14public class AmazonS3Manager {15 private String accessKey;16 private String secretKey;17 private String bucketName;18 private AmazonS3 s3client;19 private String region;20 public AmazonS3Manager(String accessKey, String secretKey, String bucketName, String region) {21 this.accessKey = accessKey;22 this.secretKey = secretKey;23 this.bucketName = bucketName;24 this.region = region;25 init();26 }27 private void init() {28 AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);29 s3client = AmazonS3ClientBuilder.standard().withRegion(region)30 .withCredentials(new AWSStaticCredentialsProvider(credentials)).build();31 }32 public void download(String key, String filePath) {33 try {34 S3Object o = s3client.getObject(new GetObjectRequest(bucketName, key));35 S3ObjectInputStream s3is = o.getObjectContent();36 File file = new File(filePath);37 FileUtils.copyInputStreamToFile(s3is, file);38 } catch (AmazonServiceException e) {39 System.err.println(e.getErrorMessage());40 System.exit(1);41 } catch (SdkClientException e) {42 System.err.println(e.getMessage());43 System.exit(1);44 } catch (IOException e) {45 System.err.println(e.getMessage());46 System.exit(1);47 }48 }49}50package com.qaprosoft.amazon;51import java.io.File;52import java.io.IOException;53import java.util.ArrayList;54import java.util.List;55import org.apache.commons.io.FileUtils;56import com.amazonaws.AmazonServiceException;57import com.amazonaws.SdkClientException;58import com.amazonaws.auth.AWSCredentials;59import com.amazonaws.auth.AWSStaticCredentialsProvider;60import com.amazonaws.auth.BasicAWSCredentials;61import com.amazonaws.services.s3.AmazonS

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Carina automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful