即使我将内容类型设置为text / html,它最终也会成为S3上的application / octet-stream。
ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(contentAsBytes);
ObjectMetadata md = new ObjectMetadata();
md.setContentLength(contentAsBytes.length);
md.setContentType("text/html");
s3.putObject(new PutObjectRequest(ARTIST_BUCKET_NAME, artistId, contentsAsStream, md));
但是,如果我将文件命名为最终使用.html
s3.putObject(new PutObjectRequest(ARTIST_BUCKET_NAME, artistId + ".html", contentsAsStream, md));
然后它有效。
我的md对象是否被忽略了?我如何以编程方式绕过这个问题,因为随着时间的推移我需要上传数千个文件,因此不能直接进入S3 UI并手动修复contentType。
答案 0 :(得分:12)
您必须在代码中执行其他操作。我刚刚使用1.9.6 S3 SDK尝试了您的代码示例,该文件获取了" text / html"内容类型。
这里是确切的(Groovy)代码:
class S3Test {
static void main(String[] args) {
def s3 = new AmazonS3Client()
def random = new Random()
def bucketName = "raniz-playground"
def keyName = "content-type-test"
byte[] contentAsBytes = new byte[1024]
random.nextBytes(contentAsBytes)
ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(contentAsBytes);
ObjectMetadata md = new ObjectMetadata();
md.setContentLength(contentAsBytes.length);
md.setContentType("text/html");
s3.putObject(new PutObjectRequest(bucketName, keyName, contentsAsStream, md))
def object = s3.getObject(bucketName, keyName)
println(object.objectMetadata.contentType)
object.close()
}
}
程序打印
text / html的
S3元数据说的相同:
以下是通过网络发送的通信(由Apache HTTP Commons调试日志记录提供):
>> PUT /content-type-test HTTP/1.1
>> Host: raniz-playground.s3.amazonaws.com
>> Authorization: AWS <nope>
>> User-Agent: aws-sdk-java/1.9.6 Linux/3.2.0-84-generic Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45
>> Date: Fri, 12 Jun 2015 02:11:16 GMT
>> Content-Type: text/html
>> Content-Length: 1024
>> Connection: Keep-Alive
>> Expect: 100-continue
<< HTTP/1.1 200 OK
<< x-amz-id-2: mOsmhYGkW+SxipF6S2+CnmiqOhwJ62WfWUkmZk4zU3rzkWCEH9P/bT1hUz27apmO
<< x-amz-request-id: 8706AE3BE8597644
<< Date: Fri, 12 Jun 2015 02:11:23 GMT
<< ETag: "6c53debeb28f1d12f7ad388b27c9036d"
<< Content-Length: 0
<< Server: AmazonS3
>> GET /content-type-test HTTP/1.1
>> Host: raniz-playground.s3.amazonaws.com
>> Authorization: AWS <nope>
>> User-Agent: aws-sdk-java/1.9.6 Linux/3.2.0-84-generic Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45
>> Date: Fri, 12 Jun 2015 02:11:23 GMT
>> Content-Type: application/x-www-form-urlencoded; charset=utf-8
>> Connection: Keep-Alive
<< HTTP/1.1 200 OK
<< x-amz-id-2: 9U1CQ8yIYBKYyadKi4syaAsr+7BV76Q+5UAGj2w1zDiPC2qZN0NzUCQNv6pWGu7n
<< x-amz-request-id: 6777433366DB6436
<< Date: Fri, 12 Jun 2015 02:11:24 GMT
<< Last-Modified: Fri, 12 Jun 2015 02:11:23 GMT
<< ETag: "6c53debeb28f1d12f7ad388b27c9036d"
<< Accept-Ranges: bytes
<< Content-Type: text/html
<< Content-Length: 1024
<< Server: AmazonS3
这也是查看source code向我们展示的行为 - 如果您设置了内容类型,则SDK不会覆盖它。
答案 1 :(得分:5)
因为您必须在发送前使用putObject方法在设置内容类型;
ObjectMetadata md = new ObjectMetadata();
InputStream myInputStream = new ByteArrayInputStream(bFile);
md.setContentLength(bFile.length);
md.setContentType("text/html");
md.setContentEncoding("UTF-8");
s3client.putObject(new PutObjectRequest(bucketName, keyName, myInputStream, md));
上传后,内容类型设置为&#34; text / html &#34;
这是一个有效的虚拟代码,检查出来,我刚刚尝试过,它正在工作;
public class TestAWS {
//TEST
private static String bucketName = "whateverBucket";
public static void main(String[] args) throws Exception {
BasicAWSCredentials awsCreds = new BasicAWSCredentials("whatever", "whatever");
AmazonS3 s3client = new AmazonS3Client(awsCreds);
try
{
String uploadFileName = "D:\\try.txt";
String keyName = "newFile.txt";
System.out.println("Uploading a new object to S3 from a file\n");
File file = new File(uploadFileName);
//bFile will be the placeholder of file bytes
byte[] bFile = new byte[(int) file.length()];
FileInputStream fileInputStream=null;
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
ObjectMetadata md = new ObjectMetadata();
InputStream myInputStream = new ByteArrayInputStream(bFile);
md.setContentLength(bFile.length);
md.setContentType("text/html");
md.setContentEncoding("UTF-8");
s3client.putObject(new PutObjectRequest(bucketName, keyName, myInputStream, md));
} catch (AmazonServiceException ase)
{
System.out.println("Caught an AmazonServiceException, which "
+ "means your request made it "
+ "to Amazon S3, but was rejected with an error response"
+ " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace)
{
System.out.println("Caught an AmazonClientException, which "
+ "means the client encountered "
+ "an internal error while trying to "
+ "communicate with S3, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
}
希望它有所帮助。
答案 2 :(得分:2)
上传文件时,AWS S3 Java客户端将尝试确定 如果还没有设置正确的内容类型。用户是 负责确保在上传时设置合适的内容类型 流。如果没有提供内容类型且无法确定 文件名,默认内容类型,“application / octet-stream”, 将被使用。
为文件提供.html扩展名提供了一种设置正确类型的方法。
根据我一直在查看的示例,您显示的代码应做您想做的事情。 :/
答案 3 :(得分:0)
Do you have any Override on the default mime content on your S3 account? Look at this link to see how to check it: How to override default Content Types.
Anyway, it looks like your S3 client fails to determine the correct mime-type by the content of the file, so it relies on the extension. octet-stream is the widely used default content mime type when a browser/servlet can't determine the mimetype: Is there any default mime type?
答案 4 :(得分:0)
我可以通过命令行轻松解决此问题,即使文件名具有正确的扩展名,我也通过aws commandline
上传html文件时遇到了类似的问题。
如之前的评论中所述,添加--content-type
参数可以解决此问题。
执行以下命令并刷新页面返回的八位字节流。
aws s3api put-object --bucket [BUCKETNAME] --body index.html --key index.html --profile [PROFILE] --acl public-read
修复:添加--content type text/html
aws s3api put-object --bucket [BUCKETNAME] --body index.html --key index.html --profile [PROFILE] --acl public-read --content-type text/html
答案 5 :(得分:0)
如果您使用的是 AWS SDK for Java 2.x,则可以在构建器模式中添加内容类型。
例如,将 Base64 编码的图像作为 JPEG 对象上传到 S3(假设您已经实例化了 S3 客户端):
byte[] stringAsByteArray = java.util.Base64.getDecoder().decode(base64EncodedString);
s3Client.putObject(
PutObjectRequest.builder().bucket("my-bucket").key("my-key").contentType("image/jpg").build(),
RequestBody.fromBytes(stringAsByteArray)
);