findify / s3mock

Embedded S3 server for easy mocking
MIT License
388 stars 107 forks source link

Version is null when updated with the same key #82

Open rrajendran opened 6 years ago

rrajendran commented 6 years ago

Version is set to null when we update the object with the same key. Ideally upon update the versionId should be updated.

client

public AmazonS3 getAmazonS3Client(boolean isTestMode,  String region,
                    Properties properties) {
        LOGGER.debug("Is S3 Test Mode? {}", isTestMode);
        AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(
                            properties.get(Constants.AWS_URL).toString(),
                            region);

         return AmazonS3ClientBuilder
                            .standard()
                            .withPathStyleAccessEnabled(true)
                            .withEndpointConfiguration(endpoint)
                            .withCredentials(new AWSStaticCredentialsProvider(
                                            new BasicAWSCredentials(properties.get(Constants.AWS_ACCESSKEY).toString(),
                                                            properties.get(Constants.AWS_SECRETKEY).toString())))
                            .build();      
    }

Implementation

public PutObjectResult putObject(String bucketName, String key, InputStream inputStream, ObjectMetadata metadata) throws AmazonServiceException {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
        if (putObjectRequest.getMetadata() == null) {
            putObjectRequest.withMetadata(new ObjectMetadata());
        }

        Map<String, String> userMetadata = putObjectRequest.getMetadata().getUserMetadata();
        userMetadata.put(MetadataKeys.CREATED_DATE, LocalDateTime.now().toString());
        userMetadata.put(LAST_MODIFIED_DATE, LocalDateTime.now().toString());
        return amazonS3.putObject(putObjectRequest);
    }

Metadata after update

{
    "userMetadata": {
        "createdDate": "2017-12-11T13:43:17.244",
        "format": "ASF_V1",
        "lastModifiedDate": "2017-12-11T13:43:17.244",
        "old": "heaven"
    },
    "httpExpiresDate": null,
    "expirationTime": null,
    "expirationTimeRuleId": null,
    "ongoingRestore": null,
    "restoreExpirationTime": null,
    "contentType": "application/octet-stream",
    "contentLength": 7,
    "contentEncoding": null,
    "lastModified": 1512999797000,
    "instanceLength": 7,
    "versionId": null,
    "storageClass": null,
    "ssecustomerKeyMd5": null,
    "requesterCharged": false,
    "contentLanguage": null,
    "serverSideEncryption": null,
    "replicationStatus": null,
    "contentDisposition": null,
    "sseawsKmsKeyId": null,
    "ssecustomerAlgorithm": null,
    "ssealgorithm": null,
    "etag": "62896a47aa5b7d82ea732195bda1b65f",
    "rawMetadata": {
        "Accept-Ranges": "bytes",
        "amz-sdk-invocation-id": "6ff85dde-6ebd-cd30-361c-e80e0703a8eb",
        "amz-sdk-retry": "0/0/500",
        "authorization": "AWS4-HMAC-SHA256 Credential=docker/20171211/us-east-2/s3/aws4_request, SignedHeaders=amz-sdk-invocation-id;amz-sdk-retry;content-length;content-type;host;user-agent;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-meta-createddate;x-amz-meta-format;x-amz-meta-lastmodifieddate;x-amz-meta-old, Signature=9c29a9ba168b4cd164b97b0040675b4a5b26bfa0ec80b77dd4acc4f8dc2ecb9e",
        "Content-Length": 7,
        "Content-MD5": "62896a47aa5b7d82ea732195bda1b65f",
        "Content-Type": "application/octet-stream",
        "ETag": "62896a47aa5b7d82ea732195bda1b65f",
        "Expect": "100-continue",
        "Host": "localhost:7001",
        "Last-Modified": 1512999797000,
        "Timeout-Access": "<function1>",
        "User-Agent": "aws-sdk-java/1.11.220 Windows_10/10.0 Java_HotSpot (TM) _64-Bit_Server_VM/25.102-b14 java/1.8.0_102 scala/2.12.4",
        "x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-PAYLOAD",
        "X-Amz-Date": "20171211T134317Z",
        "x-amz-decoded-content-length": "7"
    },
    "cacheControl": null,
    "partCount": null,
    "contentMD5": "62896a47aa5b7d82ea732195bda1b65f",
    "contentRange": null
}

S3 Mock

new S3Mock.Builder().withPort(port).withInMemoryBackend().build()

shuttie commented 6 years ago

Versions are not supported at the moment.