Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将流上载到AmazonS3_Java_Amazon S3 - Fatal编程技术网

Java 将流上载到AmazonS3

Java 将流上载到AmazonS3,java,amazon-s3,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 { InputStr

这是我面临的一个例外=

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

您得到了一个HTTP重定向响应——正如错误消息所说,您必须使用该地址,而不是实际连接的位置。或者,您可以执行以下操作:

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)

您尝试上载到的bucket是在不同于“美国标准”的地区创建的。创建S3Client对象时,需要指定正确的区域。在AWS SDK for Java的1.4版本中,您可以执行以下操作:

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();
}
AmazonS3 s3 = new AmazonS3Client();
s3.setRegion(Region.getRegion(Regions.US_WEST_2)); // or whatever region you are working with
或者你可以:

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();
}
AmazonS3 s3 = new AmazonS3Client();
s3.setRegion(Region.getRegion(Regions.US_WEST_2)); // or whatever region you are working with

你能发布线级日志吗?