private RevisionLocation zipAndUpload()

in src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java [314:411]


    private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePath sourceDirectory) throws IOException, InterruptedException, IllegalArgumentException {

        File zipFile = null;
        File versionFile;
        versionFile = new File(sourceDirectory + "/" + versionFileName);

        InputStreamReader reader = null;
        String version = null;
        try {
          reader = new InputStreamReader(new FileInputStream(versionFile), "UTF-8");
          char[] chars = new char[(int) versionFile.length() -1];
          reader.read(chars);
          version = new String(chars);
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          if(reader !=null){reader.close();}
        }

        if (version != null){
          zipFile = new File("/tmp/" + projectName + "-" + version + ".zip");
          final boolean fileCreated = zipFile.createNewFile();
          if (!fileCreated) {
            logger.println("File already exists, overwriting: " + zipFile.getPath());
          }
        } else {
          zipFile = File.createTempFile(projectName + "-", ".zip");
        }

        String key;
        File appspec;
        File dest;
        String deploymentGroupName = getDeploymentGroupNameFromEnv();
        String prefix = getS3PrefixFromEnv();
        String bucket = getS3BucketFromEnv();

        if(bucket.indexOf("/") > 0){
            throw new IllegalArgumentException("S3 Bucket field cannot contain any subdirectories.  Bucket name only!");
        }

        try {
            if (this.deploymentGroupAppspec) {
                appspec = new File(sourceDirectory + "/appspec." + deploymentGroupName + ".yml");
                if (appspec.exists()) {
                    dest = new File(sourceDirectory + "/appspec.yml");
                    FileUtils.copyFile(appspec, dest);
                    logger.println("Use appspec." + deploymentGroupName + ".yml");
                }
                if (!appspec.exists()) {
                    throw new IllegalArgumentException("/appspec." + deploymentGroupName + ".yml file does not exist" );
                }

            }

            logger.println("Zipping files into " + zipFile.getAbsolutePath());

            FileOutputStream outputStream = new FileOutputStream(zipFile);
            try {
                sourceDirectory.zip(
                        outputStream,
                        new DirScanner.Glob(this.includes, this.excludes)
                );
            } finally {
                outputStream.close();
            }

            if (prefix.isEmpty()) {
                key = zipFile.getName();
            } else {
                key = Util.replaceMacro(prefix, envVars);
                if (prefix.endsWith("/")) {
                    key += zipFile.getName();
                } else {
                    key += "/" + zipFile.getName();
                }
            }
            logger.println("Uploading zip to s3://" + bucket + "/" + key);
            PutObjectResult s3result = aws.s3.putObject(bucket, key, zipFile);

            S3Location s3Location = new S3Location();
            s3Location.setBucket(bucket);
            s3Location.setKey(key);
            s3Location.setBundleType(BundleType.Zip);
            s3Location.setETag(s3result.getETag());

            RevisionLocation revisionLocation = new RevisionLocation();
            revisionLocation.setRevisionType(RevisionLocationType.S3);
            revisionLocation.setS3Location(s3Location);

            return revisionLocation;
        } finally {
            final boolean deleted = zipFile.delete();
            if (!deleted) {
                logger.println("Failed to clean up file " + zipFile.getPath());
            }
        }
    }