我正在尝试使用Groovy脚本将文件上传到Amazon S3存储桶。我尝试了以下代码
@Grab( 'net.java.dev.jets3t:jets3t:0.9.0' )
import org.jets3t.service.impl.rest.httpclient.RestS3Service
import org.jets3t.service.security.AWSCredentials
import org.jets3t.service.model.*
import java.io.*;
bucketName='bucketname'
accessKey='accesskey'
secretKey='secretkey'
folder='D:/'
public putS3() {}
def login = new AWSCredentials( accessKey, secretKey )
def expiry = new GregorianCalendar( 2011,0,1 ).time
def s3 = new RestS3Service( login )
def bucket = new S3Bucket( bucketName )
args.each{fileName->
def key="$folder/$fileName"
def s3obj=new S3Object(bucket,newFile('D:/sample.txt'))
s3obj.key = key
println "\nUploading $fileName to $bucketName/$key"
s3obj = s3.putObject( bucket, s3obj )
def link = s3.createSignedGetUrl( bucketName, key, login, expiry, false )
println "$fileName : $link"
}
args块中的代码没有被执行。当我在Groovy Console中执行它时,它将结果显示为[]。请帮助我哪里出错了?
答案 0 :(得分:2)
我没有要测试的S3帐户,但这是基于documenation的简化示例:
import org.jets3t.service.impl.rest.httpclient.RestS3Service
import org.jets3t.service.model.S3Bucket
import org.jets3t.service.model.S3Object
import org.jets3t.service.security.AWSCredentials
@Grab('net.java.dev.jets3t:jets3t:0.9.0')
accessKey = 'accesskey'
secretKey = 'secretkey'
bucketName = 'bucketname'
fileName = 'D:\\sample.txt'
credentials = new AWSCredentials(accessKey, secretKey)
service = new RestS3Service(credentials)
bucket = new S3Bucket(bucketName)
file = new File(fileName)
fileObject = new S3Object(file)
fileObject.key = fileName
service.putObject(bucket, fileObject)
expiryTime = new Date() + 1 // 24 hours from current date
link = service.createSignedGetUrl(bucket.name, fileObject.key, expiryTime)
println "$fileName : $link"
答案 1 :(得分:1)
最近,我遇到了这个例子,当时我试图编写一个快速的代码以上传到S3存储桶,但是,我所有的尝试都以301结尾
org.jets3t.service.S3ServiceException: Service Error Message. -- ResponseCode: 301, ResponseStatus: Moved Permanently, XML Error Message: <?xml version="1.0" encoding="UTF-8"?><Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message><Endpoint>bucketname.s3.amazonaws.com</Endpoint><Bucket>bucketname</Bucket><RequestId>4CF5EDE9EF604DBB</RequestId><HostId>89KoQLvd93pXhnxJGcEaziSrSOPFRNXqbfPfY7LTe03z5rvVLAVx7UnFkts/Qe1fQ7eOWsaAL7A=</HostId></Error>
at org.jets3t.service.S3Service.putObject(S3Service.java:2358)
at org.jets3t.service.S3Service$putObject.call(Unknown Source)
at awsBucketDrop.run(awsBucketDrop.groovy:21)
起初,尽管这是一个存储桶位置问题,但我在301左右找到了参考,但是我修改了代码以交换到存储桶位置,无济于事。
import org.jets3t.service.model.S3Bucket
import org.jets3t.service.model.S3Object
import org.jets3t.service.security.AWSCredentials
@Grab('net.java.dev.jets3t:jets3t:0.9.4')
accessKey = '<key>'
secretKey = '<secret>'
bucketName = '<bucketname>'
fileName = '<fileLocation>'
credentials = new AWSCredentials(accessKey, secretKey)
service = new RestS3Service(credentials)
bucket = new S3Bucket(bucketName,"eu-west-1")
println bucket.getLocation()
file = new File(fileName)
fileObject = new S3Object(file)
fileObject.key = fileName
service.putObject(bucket, fileObject)
expiryTime = new Date() + 1 // 24 hours from current date
link = service.createSignedGetUrl(bucket.name, fileObject.key, expiryTime)
println "$fileName : $link"
现在,此sdk自2015年以来未进行过更新,因此它是一个相当老的SDK,因此我觉得它不再兼容(并且可能已将一些网址进行了硬编码),但是如果您有类似的经验,可以设法解决,让我知道。
谢谢
答案 2 :(得分:0)
如果您遇到这种情况并且需要有效的常规方法,那么我已经对this code进行了宽松的修改,使其成为简单的常规方法。
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
import java.io.IOException;
Regions clientRegion = Regions.EU_WEST_1;
String bucketName = "bucketname";
String stringObjKeyName = "stringObjKeyName";
String fileObjKeyName = "fileObjKeyName";
String fileName = "fileLocation";
try {
//This code expects that you have AWS credentials set up per:
// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.build();
// Upload a text string as a new object.
s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");
// Upload a file as a new object with ContentType and title specified.
PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("plain/text");
metadata.addUserMetadata("x-amz-meta-title", "someTitle");
request.setMetadata(metadata);
s3Client.putObject(request);
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}