简体   繁体   中英

Uploading stream to 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));
}

This is the exception that i am facing =

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)

The bucket that you are trying to upload to was created in a different region than "US Standard". You will need to specify the correct region when you create the S3Client object. In the 1.4 release of the AWS SDK for Java, you could do:

AmazonS3 s3 = new AmazonS3Client();
s3.setRegion(Region.getRegion(Regions.US_WEST_2)); // or whatever region you are working with

or you could do:

AmazonS3 s3 = Region.getRegion(Regions.US_WEST_2)
                    .createClient(AmazonS3Client.class, credentials, config);

You got a HTTP redirect response - and as error message says you have to use that address, not where you actually connect. Or you can do the following:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM