java.net.URLConnection conn = null;
try
{
conn = new URL(track.getStreamUrl()).openConnection();
}
catch(MalformedURLException e1)
{
e1.printStackTrace();
}
catch(IOException e1)
{
e1.printStackTrace();
}
try
{
InputStream is = conn.getInputStream();
byte[] contentbytes = getBytesFromInputStream(is);
Long contentLength = Long.valueOf(contentbytes.length);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(contentLength);
InputStream is2 = conn.getInputStream();
s3.putObject(new PutObjectRequest(bucketName, my_key, is2, metadata));
}
这是我面临的例外=
Exception in thread "AWT-EventQueue-0" AmazonS3Exception: Status Code: 301, AWS Service: Amazon S3, AWS Request ID: 79B972B0038C4F0D, AWS Error Code: PermanentRedirect, AWS Error Message: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint., S3 Extended Request ID: l4fTSi20J9Ht9DY3ECgRTZ9HL0d7LS+rkmnhe4IG8tzy6TllEI7EO2bu/FDVUWOe
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:644)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:338)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:190)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2974)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1149)
at S3TransferProgressSample$1$1.actionPerformed(S3TransferProgressSample.java:208)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
最佳答案
您尝试上传到的存储桶是在与“美国标准”不同的区域中创建的。创建S3Client对象时,需要指定正确的区域。在适用于Java的AWS开发工具包1.4版本中,您可以执行以下操作:
AmazonS3 s3 = new AmazonS3Client();
s3.setRegion(Region.getRegion(Regions.US_WEST_2)); // or whatever region you are working with
或者你可以这样做:
AmazonS3 s3 = Region.getRegion(Regions.US_WEST_2)
.createClient(AmazonS3Client.class, credentials, config);
关于java - 将流上传到Amazon S3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15591906/