我正在使用Java将文件上传到S3 - 这是我到目前为止所得到的:
AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("XX","YY"));
List<Bucket> buckets = s3.listBuckets();
s3.putObject(new PutObjectRequest(buckets.get(0).getName(), fileName, stream, new ObjectMetadata()));
Run Code Online (Sandbox Code Playgroud)
正在上传文件,但在我未设置内容长度时会引发警告:
Run Code Online (Sandbox Code Playgroud)com.amazonaws.services.s3.AmazonS3Client putObject: No content length specified for stream > data. Stream contents will be buffered in memory and could result in out of memory errors.
这是我上传的文件,stream变量是一个InputStream,我可以从中得到这样的字节数组:IOUtils.toByteArray(stream).
所以,当我尝试设置内容长度和MD5(从这里取得)像这样:
// get MD5 base64 hash
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(IOUtils.toByteArray(stream));
byte[] resultByte = messageDigest.digest();
String hashtext = new String(Hex.encodeHex(resultByte));
ObjectMetadata meta = new ObjectMetadata(); …Run Code Online (Sandbox Code Playgroud) 当我使用amazon s3 SDK从Nexus 6上传文件时,它会抛出com.amazonaws.AmazonClientException:更多数据读取(4567265)比预期(4561427)异常.
但是当我从Moto G4上传图像并使用相同的代码时,它每次都会上传.
请帮我解决这个问题.
这是我的代码供参考:
private void uploadingScreenshot(String filePath)
{
File file = new File(filePath);
if (file.exists()) {
final String serverPath = S3Util.getMediaPath(Utility.MediaType.SCREENSHOT, false, "");
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(file.length());
S3Util.uploadMedia(SharedFolderDetailActivity.this, file, serverPath, meta, new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
switch (state) {
case COMPLETED: {
String path = S3Constants.BUCKET_URL + serverPath;
callTookScreenshotNotifierWS(path);
}
break;
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) 在 S3 存储桶中不存在密钥时获取消息。我检索该存储桶中的所有对象并将这些键与给定的搜索键匹配。如果可用,则返回 URL-String,否则返回消息“指定的键不存在”。
他们在访问密钥时是否还有其他提高性能的方法,这在 S3 存储桶中不可用。
这是我的代码:
public class S3Objects {
static Properties props = new Properties();
static InputStream resourceAsStream;
static {
ClassLoader classLoader = new S3Objects().getClass().getClassLoader();
resourceAsStream = classLoader.getResourceAsStream("aws.properties");
try {
props.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, AmazonServiceException, AmazonClientException, InterruptedException {
AWSCredentials awsCreds = new
BasicAWSCredentials(props.getProperty("accessKey"), props.getProperty("secretKey"));
// PropertiesCredentials(resourceAsStream);
AmazonS3 s3Client = new AmazonS3Client( awsCreds );
String s3_BucketName = props.getProperty("bucketname");
String folderPath_fileName = props.getProperty("path");
//uploadObject(s3Client, s3_BucketName, folderPath_fileName);
//downloadObject(s3Client, …Run Code Online (Sandbox Code Playgroud)