How to write a string to Amazon S3 bucket?

后端 未结 7 549
花落未央
花落未央 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: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;
            }
        }
    

提交回复
热议问题