How to write a string to Amazon S3 bucket?

后端 未结 7 520
花落未央
花落未央 2020-12-10 10:42

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

相关标签:
7条回答
  • 2020-12-10 10:45

    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);
    
    0 讨论(0)
  • 2020-12-10 10:45

    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-

    0 讨论(0)
  • 2020-12-10 10:47

    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);
    }
    
    0 讨论(0)
  • 2020-12-10 10:50

    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.

    0 讨论(0)
  • 2020-12-10 10:54

    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);
    
    0 讨论(0)
  • 2020-12-10 10:56

    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;
            }
        }
    
    0 讨论(0)
提交回复
热议问题