Sourabh
Sourabh

Reputation: 4765

How to write a string to Amazon S3 bucket?

How can I add a string as a file on amazon s3? From whaterver I searched, I got to know that we can upload a file to s3. What is the best way to upload data without creating file?

Upvotes: 49

Views: 88037

Answers (7)

Jonik
Jonik

Reputation: 81792

What is the best way to upload data without creating file?

If you meant without creating a file on S3, well, you can't really do that. On Amazon S3, the only way to store data is as files, or using more accurate terminology, objects. An object can contain from 1 byte zero bytes to 5 terabytes of data, and is stored in a bucket. Amazon's S3 homepage lays out the basic facts quite clearly. (For other data storing options on AWS, you might want to read e.g. about SimpleDB.)

If you meant without creating a local temporary file, then the answer depends on what library/tool you are using. (As RickMeasham suggested, please add more details!) With the s3cmd tool, for example, you can't skip creating temp file, while with the JetS3t Java library uploading a String directly would be easy:

// (First init s3Service and testBucket)
S3Object stringObject = new S3Object("HelloWorld.txt", "Hello World!");
s3Service.putObject(testBucket, stringObject);

Edit (2023): The JetS3t library is no longer maintained so I'm certainly not recommending using it. Maybe try the official AWS Java SDK along these lines.

Upvotes: 11

barryku
barryku

Reputation: 2584

The sample code at https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html works for me.

s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");

Looks like this was added around 1.11.20, so make sure you are using that or new version of SDK.

https://javadoc.io/doc/com.amazonaws/aws-java-sdk-s3/1.11.20/com/amazonaws/services/s3/AmazonS3.html#putObject-java.lang.String-java.lang.String-java.lang.String-

Upvotes: 0

Rony
Rony

Reputation: 142

There is a simple way to do it with PHP, simply send the string as the body of the object, specifying the name of the new file in the key -

$s3->putObject(array(
        'Bucket'       => [Bucket name],
        'Key'          => [path/to/file.ext],
        'Body'         => [Your string goes here],
        'ContentType'  => [specify mimetype if you want],
    ));

This will create a new file according to the specified key, which has a content as specified in the string.

Upvotes: 6

Rex NFX
Rex NFX

Reputation: 473

There is an overload for the AmazonS3.putObject method that accepts the bucket string, a key string, and a string of text content. I hadn't seen mention of it on stack overflow so I'm putting this here. It's going to be similar @Jonik's answer, but without the additional dependency.

AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
s3client.putObject(bucket, key, contents);

Upvotes: 29

Michael M
Michael M

Reputation: 8753

This works for me:

public static PutObjectResult WriteString(String bucket, String key, String stringToWrite, AmazonS3Client s3Client) {
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentMD5(new String(com.amazonaws.util.Base64.encode(DigestUtils.md5(stringToWrite))));
    meta.setContentLength(stringToWrite.length());
    InputStream stream = new ByteArrayInputStream(stringToWrite.getBytes(StandardCharsets.UTF_8));
    return s3Client.putObject(bucket, key, stream, meta);
}

Upvotes: 2

Ivan
Ivan

Reputation: 41

If you're using java, check out https://ivan-site.com/2015/11/interact-with-s3-without-temp-files/

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.nio.charset.StandardCharsets;

class S3StreamJacksonTest {
    private static final String S3_BUCKET_NAME = "bucket";
    private static final String S3_KEY_NAME = "key";
    private static final String CONTENT_TYPE = "application/json";

    private static final AmazonS3 AMAZON_S3 = new AmazonS3Client();
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private static final TestObject TEST_OBJECT = new TestObject("test", 123, 456L);

    public void testUploadWithStream() throws JsonProcessingException {
        String fileContentString = OBJECT_MAPPER.writeValueAsString(TEST_OBJECT);
        byte[] fileContentBytes = fileContentString.getBytes(StandardCharsets.UTF_8);
        InputStream fileInputStream = new ByteArrayInputStream(fileContentBytes);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(CONTENT_TYPE);
        metadata.setContentLength(fileContentBytes.length);
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                S3_BUCKET_NAME, S3_KEY_NAME, fileInputStream, metadata);
        AMAZON_S3.putObject(putObjectRequest);
    }
}

Upvotes: 4

Paul Taylor
Paul Taylor

Reputation: 13110

Doesn't look as nice, but here is how you can do it using Amazons Java client, probably what JetS3t does behind the scenes anyway.

private boolean putArtistPage(AmazonS3 s3,String bucketName, String key, String webpage)
    {
        try
        {
            byte[]                  contentAsBytes = webpage.getBytes("UTF-8");
            ByteArrayInputStream    contentsAsStream      = new ByteArrayInputStream(contentAsBytes);
            ObjectMetadata          md = new ObjectMetadata();
            md.setContentLength(contentAsBytes.length);
            s3.putObject(new PutObjectRequest(bucketname, key, contentsAsStream, md));
            return true;
        }
        catch(AmazonServiceException e)
        {
            log.log(Level.SEVERE, e.getMessage(), e);
            return false;
        }
        catch(Exception ex)
        {
            log.log(Level.SEVERE, ex.getMessage(), ex);
            return false;
        }
    }

Upvotes: 13

Related Questions