上传流到亚马逊s3

时间:2013-03-23 20:50:23

标签: java amazon-s3

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)

2 个答案:

答案 0 :(得分:5)

您尝试上传到的存储桶是在与“美国标准”不同的区域中创建的。创建S3Client对象时,需要指定正确的区域。在AWS SDK for Java的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);

答案 1 :(得分:0)

您有一个HTTP重定向响应 - 并且错误消息显示您必须使用该地址,而不是您实际连接的位置。或者您可以执行以下操作:

java.net.HttpURLConnection conn = null;
try
{
    URL url = new URL(track.getStreamUrl());
    conn = (HttpURLConnection)url.openConnection();
    conn.setFollowRedirects(true);
}
catch(MalformedURLException e1)
{
    e1.printStackTrace();
}
catch(IOException e1)
{

    e1.printStackTrace();
}