2011-04-06 80 views
2

嗨,我试图访问S3使用jets3t库。 我只是其次AWS支持的示例代码(here's url) 这里是我的简单的代码如何使用jets3t访问S3?

import org.jets3t.service.S3Service; 
import org.jets3t.service.S3ServiceException; 
import org.jets3t.service.impl.rest.httpclient.RestS3Service; 
import org.jets3t.service.model.S3Bucket; 
import org.jets3t.service.security.AWSCredentials; 

public class Test1 { 

    /** 
    * @param args 
    */ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    String awsAccessKey = "******************"; 
    String awsSecretKey = "*********************************"; 

    System.out.println("0"); 
    AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey); 

    try{ 
    S3Service s3Service = new RestS3Service(awsCredentials); 

    System.out.println("1"); 

    S3Bucket testBucket = s3Service.getOrCreateBucket("first_test_bucket"); 
    System.out.println("Created test bucket: " + testBucket.getName()); 

    S3Bucket[] myBuckets = s3Service.listAllBuckets(); 

    System.out.println("2"); 

    System.out.println("How many buckets to I have in S3? " + myBuckets.length); 

    System.out.println("3"); 

    } 
    catch (Exception e){ 

     System.out.println("exception: " + e.toString()); 

    } 

} 

}

但我遇到了一个问题,这样的

0 
15:24:39 DEBUG (service.Jets3tProperties - getBoolProperty:315) - s3service.https-only=true 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - storage-service.internal-error-retry-max=5 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - httpclient.connection-timeout-ms=60000 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - httpclient.socket-timeout-ms=60000 
15:24:39 DEBUG (service.Jets3tProperties - getBoolProperty:315) - httpclient.stale-checking-enabled=true 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - httpclient.max-connections=20 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - httpclient.max-connections-per-host=0 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - httpclient.useragent=null 
15:24:39 DEBUG (utils.RestUtils - initHttpConnection:320) - Setting user agent string: JetS3t/0.8.0 (Windows 7/6.1; x86; ko; JVM 1.6.0_20) 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - httpclient.retry-max=5 
15:24:39 DEBUG (service.Jets3tProperties - getLongProperty:279) - httpclient.connection-manager-timeout=0 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=null 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.default-storage-class=null 
15:24:39 DEBUG (service.Jets3tProperties - getBoolProperty:315) - httpclient.proxy-autodetect=true 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - devpay.user-token=null 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - devpay.product-token=null 
15:24:39 DEBUG (service.Jets3tProperties - getBoolProperty:315) - httpclient.requester-pays-buckets-enabled=false 
1 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.default-bucket-location=US 
15:24:39 DEBUG (httpclient.RestStorageService - listAllBucketsImpl:1180) - Listing all buckets for user: AKIAI6TCJ7BDPCPTHV2A 
15:24:39 DEBUG (service.Jets3tProperties - getBoolProperty:315) - s3service.disable-dns-buckets=false 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint-virtual-path= 
15:24:39 DEBUG (service.Jets3tProperties - getIntProperty:297) - s3service.s3-endpoint-https-port=443 
15:24:39 DEBUG (httpclient.RestStorageService - setupConnection:1053) - S3 URL: https://s3.amazonaws.com:443/ 
15:24:39 DEBUG (httpclient.RestStorageService - performRequest:340) - Performing GET request for 'https://s3.amazonaws.com/', expecting response codes: [200] 
15:24:39 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:591) - Adding authorization for Access Key 'AKIAI6TCJ7BDPCPTHV2A'. 
15:24:39 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:24:39 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:643) - Canonical string ('|' is a newline): GET|||Wed, 06 Apr 2011 06:24:39 GMT|/ 
15:25:01 DEBUG (utils.RestUtils$1 - retryMethod:345) - Retrying GET request with path '/' - attempt 1 of 5 
15:25:01 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:591) - Adding authorization for Access Key 'AKIAI6TCJ7BDPCPTHV2A'. 
15:25:01 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:25:01 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:643) - Canonical string ('|' is a newline): GET|||Wed, 06 Apr 2011 06:25:01 GMT|/ 
15:25:01 INFO (httpclient.HttpMethodDirector - executeWithRetry:439) - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 
15:25:01 INFO (httpclient.HttpMethodDirector - executeWithRetry:445) - Retrying request 
15:25:22 DEBUG (utils.RestUtils$1 - retryMethod:345) - Retrying GET request with path '/' - attempt 2 of 5 
15:25:22 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:591) - Adding authorization for Access Key 'AKIAI6TCJ7BDPCPTHV2A'. 
15:25:22 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:25:22 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:643) - Canonical string ('|' is a newline): GET|||Wed, 06 Apr 2011 06:25:22 GMT|/ 
15:25:22 INFO (httpclient.HttpMethodDirector - executeWithRetry:439) - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 
15:25:22 INFO (httpclient.HttpMethodDirector - executeWithRetry:445) - Retrying request 
15:25:43 DEBUG (utils.RestUtils$1 - retryMethod:345) - Retrying GET request with path '/' - attempt 3 of 5 
15:25:43 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:591) - Adding authorization for Access Key 'AKIAI6TCJ7BDPCPTHV2A'. 
15:25:43 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:25:43 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:643) - Canonical string ('|' is a newline): GET|||Wed, 06 Apr 2011 06:25:43 GMT|/ 
15:25:43 INFO (httpclient.HttpMethodDirector - executeWithRetry:439) - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 
15:25:43 INFO (httpclient.HttpMethodDirector - executeWithRetry:445) - Retrying request 
    15:26:04 DEBUG (utils.RestUtils$1 - retryMethod:345) - Retrying GET request with path '/' - attempt 4 of 5 
15:26:04 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:591) - Adding authorization for Access Key 'AKIAI6TCJ7BDPCPTHV2A'. 
15:26:04 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:26:04 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:643) - Canonical string ('|' is a newline): GET|||Wed, 06 Apr 2011 06:26:04 GMT|/ 
15:26:04 INFO (httpclient.HttpMethodDirector - executeWithRetry:439) - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 
15:26:04 INFO (httpclient.HttpMethodDirector - executeWithRetry:445) - Retrying request 
15:26:25 DEBUG (utils.RestUtils$1 - retryMethod:345) - Retrying GET request with path '/' - attempt 5 of 5 
15:26:25 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:591) - Adding authorization for Access Key 'AKIAI6TCJ7BDPCPTHV2A'. 
15:26:25 DEBUG (service.Jets3tProperties - getStringProperty:261) - s3service.s3-endpoint=s3.amazonaws.com 
15:26:25 DEBUG (httpclient.RestStorageService - authorizeHttpRequest:643) - Canonical string ('|' is a newline): GET|||Wed, 06 Apr 2011 06:26:25 GMT|/ 
15:26:25 INFO (httpclient.HttpMethodDirector - executeWithRetry:439) - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect 
15:26:25 INFO (httpclient.HttpMethodDirector - executeWithRetry:445) - Retrying request 
15:26:46 DEBUG (httpclient.RestStorageService - performRequest:538) - Releasing HttpClient connection after error: Connection timed out: connect 
exception: org.jets3t.service.S3ServiceException: Request Error. GET '/' 

我想这因为我在我的公司处理了这段代码,所以是一个关于防火墙的问题。有人知道这个问题吗?帮帮我!

+0

我希望访问密钥和密钥是假的... – 2011-04-06 06:52:29

+0

你的密钥在这篇文章的旧版本中仍然可见。您最好删除整个线程或更好地登录到Amazon AWS并尽快更改您的凭据。 – 2011-04-06 09:32:51

回答

3

在你的 - 不那么容易阅读 - 日志中有一些“连接超时:连接异常:...”。所以,是的,这可能是因为您在代理后面(Amazon S3 Rest服务在端口443上侦听)。

您可能想要配置JetS3t以使用代理。 创建jets3t.properties属性文件并添加以下属性:

httpclient.proxy-host=your proxy host 
httpclient.proxy-port=your proxy port 
httpclient.proxy-user=proxy user 
httpclient.proxy-password=password 

更多的信息在这里:http://jets3t.s3.amazonaws.com/toolkit/configuration.html#jets3t

而且,请注意,有一个莫名其妙稳定S3测试桩,您可以使用来进行测试: https://github.com/technoweenie/parkplace

+0

非常感谢,卢西亚诺先生,也为您的建议!这是我的错误,暴露了我的秘密,访问密钥!我已经删除了密钥!而我解决了这个代理问题,虽然我徘徊了一些问题,如添加属性到eclipse项目! – truelifer 2011-04-07 00:55:42

3

你需要设置一个文本文件格式的JetS3t配置和给予的JetS3t类属性如下:

Jets3tProperties jets3=new Jets3tProperties(); 
jets3.loadAndReplaceProperties(new FileInputStream("/root/Desktop/jets3t.properties"), "jets3t.properties");