Example usage for com.amazonaws.services.devicefarm.model Upload getUrl

List of usage examples for com.amazonaws.services.devicefarm.model Upload getUrl

Introduction

In this page you can find the example usage for com.amazonaws.services.devicefarm.model Upload getUrl.

Prototype


public String getUrl() 

Source Link

Document

The pre-signed Amazon S3 URL that was used to store a file through a corresponding PUT request.

Usage

From source file:org.jenkinsci.plugins.awsdevicefarm.AWSDeviceFarm.java

License:Open Source License

/**
 * Private method to handle upload apps and tests to Device Farm.
 *
 * @param file        The file to upload.
 * @param project     TheDevice Farm project to upload to.
 * @param uploadType  The type of upload (app/test/etc.).
 * @param synchronous Whether or not to wait for the upload to complete before returning.
 * @return The Device Farm Upload object.
 * @throws IOException//  w ww . j a  va 2  s .  c o m
 * @throws AWSDeviceFarmException
 */
private Upload upload(File file, Project project, AWSDeviceFarmUploadType uploadType, Boolean synchronous)
        throws InterruptedException, IOException, AWSDeviceFarmException {
    CreateUploadRequest appUploadRequest = new CreateUploadRequest().withName(file.getName())
            .withProjectArn(project.getArn()).withContentType("application/octet-stream")
            .withType(uploadType.toString());
    Upload upload = api.createUpload(appUploadRequest).getUpload();

    CloseableHttpClient httpClient = HttpClients.createSystem();
    HttpPut httpPut = new HttpPut(upload.getUrl());
    httpPut.setHeader("Content-Type", upload.getContentType());

    FileEntity entity = new FileEntity(file);
    httpPut.setEntity(entity);

    writeToLog(String.format("Uploading %s to S3", file.getName()));
    HttpResponse response = httpClient.execute(httpPut);
    if (response.getStatusLine().getStatusCode() != 200) {
        throw new AWSDeviceFarmException(String.format("Upload returned non-200 responses: %d",
                response.getStatusLine().getStatusCode()));
    }

    if (synchronous) {
        while (true) {
            GetUploadRequest describeUploadRequest = new GetUploadRequest().withArn(upload.getArn());
            GetUploadResult describeUploadResult = api.getUpload(describeUploadRequest);
            String status = describeUploadResult.getUpload().getStatus();

            if ("SUCCEEDED".equalsIgnoreCase(status)) {
                writeToLog(String.format("Upload %s succeeded", file.getName()));
                break;
            } else if ("FAILED".equalsIgnoreCase(status)) {
                writeToLog(String.format("Error message from device farm: '%s'",
                        describeUploadResult.getUpload().getMetadata()));
                throw new AWSDeviceFarmException(String.format("Upload %s failed!", upload.getName()));
            } else {
                try {
                    writeToLog(String.format("Waiting for upload %s to be ready (current status: %s)",
                            file.getName(), status));
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    writeToLog(String.format("Thread interrupted while waiting for the upload to complete"));
                    throw e;
                }
            }
        }
    }

    return upload;
}