/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.resource.transport.aws.s3;

import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import org.gradle.internal.IoActions;
import org.gradle.internal.resource.ReadableContent;
import org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData;
import org.gradle.internal.resource.metadata.ExternalResourceMetaData;
import org.gradle.internal.resource.transfer.ExternalResourceConnector;
import org.gradle.internal.resource.transfer.ExternalResourceReadResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

public class S3ResourceConnector implements ExternalResourceConnector {

    private static final Logger LOGGER = LoggerFactory.getLogger(S3ResourceConnector.class);
    private final S3Client s3Client;

    public S3ResourceConnector(S3Client s3Client) {
        this.s3Client = s3Client;
    }

    @Override
    public List<String> list(URI parent) {
        LOGGER.debug("Listing parent resources: {}", parent);
        return s3Client.listDirectChildren(parent);
    }

    @Override
    public ExternalResourceReadResponse openResource(URI location, boolean revalidate) {
        LOGGER.debug("Attempting to get resource: {}", location);
        S3Object s3Object = s3Client.getResource(location);
        if (s3Object == null) {
            return null;
        }
        return new S3Resource(s3Object, location);
    }

    @Override
    public ExternalResourceMetaData getMetaData(URI location, boolean revalidate) {
        LOGGER.debug("Attempting to get resource metadata: {}", location);
        S3Object s3Object = s3Client.getMetaData(location);
        if (s3Object == null) {
            return null;
        }
        try {
            ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
            return new DefaultExternalResourceMetaData(location,
                objectMetadata.getLastModified().getTime(),
                objectMetadata.getContentLength(),
                objectMetadata.getContentType(),
                objectMetadata.getETag(),
                null); // Passing null for sha1 - TODO - consider using the etag which is an MD5 hash of the file (when less than 5Gb)
        } finally {
            IoActions.closeQuietly(s3Object);
        }
    }

    @Override
    public void upload(ReadableContent resource, URI destination) throws IOException {
        LOGGER.debug("Attempting to upload stream to : {}", destination);
        InputStream inputStream = resource.open();
        try {
            s3Client.put(inputStream, resource.getContentLength(), destination);
        } finally {
            inputStream.close();
        }
    }
}

@@ -770,16 +770,57 @@
 Object;%0A
+import com.google.common.io.ByteStreams;%0A
 import o
@@ -2988,32 +2988,1000 @@
     %7D finally %7B%0A
+            discardEmptyContentAndClose(s3Object);%0A        %7D%0A    %7D%0A%0A    private static void discardEmptyContentAndClose(S3Object s3Object) %7B%0A        // Consume the content stream to avoid warning from S3 SDK. The response should have only 1 byte there because Range header was specified.%0A        try %7B%0A            long downloadedContentLength = ByteStreams.exhaust(s3Object.getObjectContent());%0A            if (downloadedContentLength %3E 1L) %7B%0A                // This may happen if the endpoint ignores Range HTTP header for whatever reason.%0A                LOGGER.info(%22Downloaded %7B%7D bytes of the object content for metadata request which is too much.%22, downloadedContentLength);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            // Don't complain loudly to the user about the error there because we were discarding the response anyway.%0A            LOGGER.debug(%22Exception while consuming empty object content from metadata request%22, e);%0A        %7D finally %7B%0A
             IoAc

Discard the object body when doing metadata request to S3