package org.getopentest.actions.s3;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectMetadataRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import java.util.Date;
import org.getopentest.base.TestAction;

/**
 * Read the metadata for an S3 object.
 */
public class GetS3Metadata extends TestAction {

    @Override
    public void run() {
        super.run();

        String awsCredentialsProfile = this.readStringArgument("awsProfile", null);
        String accessKey = this.readStringArgument("accessKey", null);
        String secretKey = this.readStringArgument("secretKey", null);
        String bucket = this.readStringArgument("bucket");
        String objectKey = this.readStringArgument("objectKey");

        AmazonS3 s3Client = null;

        if (awsCredentialsProfile != null) {
            s3Client = new AmazonS3Client(
                    new ProfileCredentialsProvider(awsCredentialsProfile));
        } else if (accessKey != null && secretKey != null) {
            s3Client = new AmazonS3Client(
                    new AWSStaticCredentialsProvider(
                            new BasicAWSCredentials(accessKey, secretKey)));
        } else {
            s3Client = new AmazonS3Client();
        }

        ObjectMetadata metadata = s3Client.getObjectMetadata(
                new GetObjectMetadataRequest(bucket, objectKey));

        try {
            Date expirationTime = metadata.getExpirationTime();
            if (expirationTime != null) {
                this.writeOutput("expirationTime", metadata.getExpirationTime().getTime());
            } else {
                this.writeOutput("expirationTime", null);
            }
            this.writeOutput("lastModified", metadata.getLastModified().getTime());
            this.writeOutput("userMetadata", metadata.getUserMetadata());
            this.writeOutput("size", metadata.getContentLength());
            this.writeOutput("storageClass", metadata.getStorageClass());
            this.writeOutput("versionId", metadata.getVersionId());
        } catch (Exception ex) {
            throw new RuntimeException(String.format(
                    "Failed to get object metadata for object key %s in bucket %s",
                    objectKey,
                    bucket), ex);
        }
    }
}
