Search in sources :

Example 51 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project dataverse by IQSS.

the class S3AccessIO method savePathAsAux.

@Override
public // this method copies a local filesystem Path into this DataAccess Auxiliary location:
void savePathAsAux(Path fileSystemPath, String auxItemTag) throws IOException {
    if (!this.canWrite()) {
        open(DataAccessOption.WRITE_ACCESS);
    }
    String destinationKey = getDestinationKey(auxItemTag);
    try {
        File inputFile = fileSystemPath.toFile();
        s3.putObject(new PutObjectRequest(bucketName, destinationKey, inputFile));
    } catch (AmazonClientException ase) {
        logger.warning("Caught an AmazonServiceException in S3AccessIO.savePathAsAux():    " + ase.getMessage());
        throw new IOException("S3AccessIO: Failed to save path as an auxiliary object.");
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 52 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project dataverse by IQSS.

the class S3AccessIO method savePath.

// StorageIO method for copying a local Path (for ex., a temp file), into this DataAccess location:
@Override
public void savePath(Path fileSystemPath) throws IOException {
    long newFileSize = -1;
    if (!this.canWrite()) {
        open(DataAccessOption.WRITE_ACCESS);
    }
    try {
        File inputFile = fileSystemPath.toFile();
        if (dvObject instanceof DataFile) {
            s3.putObject(new PutObjectRequest(bucketName, key, inputFile));
            newFileSize = inputFile.length();
        } else {
            throw new IOException("DvObject type other than datafile is not yet supported");
        }
    } catch (SdkClientException ioex) {
        String failureMsg = ioex.getMessage();
        if (failureMsg == null) {
            failureMsg = "S3AccessIO: Unknown exception occured while uploading a local file into S3Object";
        }
        throw new IOException(failureMsg);
    }
    // if it has uploaded successfully, we can reset the size
    // of the object:
    setSize(newFileSize);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) SdkClientException(com.amazonaws.SdkClientException) IOException(java.io.IOException) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 53 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project sandbox by irof.

the class S3MultipartUploadTest method 比較用の通常のputObject.

@Ignore
@Test
public void 比較用の通常のputObject() throws Exception {
    byte[] bytes = new byte[5 * Constants.MB + 1];
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentLength(bytes.length);
    PutObjectRequest putObjectRequest = new PutObjectRequest("irof-sandbox", "S3MultipartUpload/basic-5MB.dat", new ByteArrayInputStream(bytes), meta);
    PutObjectResult result = new AmazonS3Client().putObject(putObjectRequest);
    logger.info(result.getETag());
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 54 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project sandbox by irof.

the class S3Test method putWithInputStream.

@Test
public void putWithInputStream() throws Exception {
    try (InputStream input = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8))) {
        ObjectMetadata metaData = new ObjectMetadata();
        // 可能なら設定する。設定しなかったらデフォルト値が使われる。
        metaData.setContentType("text/plain");
        // 設定しなくても計算されるぽいのでスルーでよさげ
        // metaData.setContentMD5("CY9rzUYh03PK3k6DJie09g==");
        // オプションだけど、できるかぎり設定する。
        // 設定しないとWARNログ出ちゃうし、違う値を設定すると例外出る。
        // metaData.setContentLength(3);
        PutObjectRequest request = new PutObjectRequest("irof-sandbox", "byteFile.txt", input, metaData);
        PutObjectResult result = s3.putObject(request);
        assertThat(result.getETag(), is("098f6bcd4621d373cade4e832627b4f6"));
    }
}
Also used : PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 55 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project stocator by CODAIT.

the class COSAPIClient method createObject.

@Override
public FSDataOutputStream createObject(String objName, String contentType, Map<String, String> metadata, Statistics statistics) throws IOException {
    LOG.debug("Create object {}", objName);
    try {
        String objNameWithoutBuket = objName;
        if (objName.startsWith(mBucket + "/")) {
            objNameWithoutBuket = objName.substring(mBucket.length() + 1);
        }
        if (blockUploadEnabled) {
            return new FSDataOutputStream(new COSBlockOutputStream(this, objNameWithoutBuket, new SemaphoredDelegatingExecutor(threadPoolExecutor, blockOutputActiveBlocks, true), partSize, blockFactory, contentType, new WriteOperationHelper(objNameWithoutBuket), metadata), null);
        }
        if (!contentType.equals(Constants.APPLICATION_DIRECTORY)) {
            return new FSDataOutputStream(new COSOutputStream(mBucket, objName, mClient, contentType, metadata, transfers, this), statistics);
        } else {
            final InputStream im = new InputStream() {

                @Override
                public int read() throws IOException {
                    return -1;
                }
            };
            final ObjectMetadata om = new ObjectMetadata();
            om.setContentLength(0L);
            om.setContentType(contentType);
            om.setUserMetadata(metadata);
            // Remove the bucket name prefix from key path
            if (objName.startsWith(mBucket + "/")) {
                objName = objName.substring(mBucket.length() + 1);
            }
            /*
        if (!objName.endsWith("/")) {
          objName = objName + "/";
        }*/
            LOG.debug("bucket: {}, key {}", mBucket, objName);
            PutObjectRequest putObjectRequest = new PutObjectRequest(mBucket, objName, im, om);
            Upload upload = transfers.upload(putObjectRequest);
            upload.waitForUploadResult();
            OutputStream fakeStream = new OutputStream() {

                @Override
                public void write(int b) throws IOException {
                }

                @Override
                public void close() throws IOException {
                    super.close();
                }
            };
            return new FSDataOutputStream(fakeStream, statistics);
        }
    } catch (InterruptedException e) {
        throw new InterruptedIOException("Interrupted creating " + objName);
    } catch (IOException e) {
        LOG.error(e.getMessage());
        throw e;
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) COSInputStream(com.ibm.stocator.fs.cos.COSInputStream) InputStream(java.io.InputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) Upload(com.amazonaws.services.s3.transfer.Upload) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)140 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)80 ByteArrayInputStream (java.io.ByteArrayInputStream)63 Test (org.junit.Test)61 File (java.io.File)35 IOException (java.io.IOException)32 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)29 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)23 Upload (com.amazonaws.services.s3.transfer.Upload)23 AmazonClientException (com.amazonaws.AmazonClientException)20 InputStream (java.io.InputStream)18 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)14 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)14 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)14 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)13 HashMap (java.util.HashMap)12 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)10 AmazonServiceException (com.amazonaws.AmazonServiceException)9 AmazonS3 (com.amazonaws.services.s3.AmazonS3)9 Exchange (org.apache.camel.Exchange)8