使用Groovy(Groovy版本:2.4.11 JVM:1.8),我正在使用Amazon Java SDK(最新版本是1.11.147) 0_112供應商:Oracle Corporation操作系統:Mac OS X)將文件上傳到S3。使用Amazon的文檔中的說明獲取Transfer Manager,我能夠將內容從一個存儲桶複製到另一個存儲桶。但是,上傳總是失敗。我嘗試了以下3種方法。我有葡萄搶,因爲我在這個堆棧溢出後Apache PoolingHttpClientConnectionManager throwing illegal state exception使用Groovy中的「連接池關閉」,AWS S3 Java SDK上載失敗
@Grapes([
@Grab(group='com.amazonaws', module='aws-java-sdk', version='1.11.147'),
// https://mvnrepository.com/artifact/org.apache.commons/commons-compress
@Grab(group='org.apache.commons', module='commons-compress', version='1.13'),
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.3'),
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore
@Grab(group='org.apache.httpcomponents', module='httpcore', version='4.4.6')
])
// creds is String saying which ~/.aws/credentials profile to use
def credentials = new ProfileCredentialsProvider(creds)
def s3client = AmazonS3ClientBuilder.standard().
withCredentials(credentials).
withRegion(region).
build()
def tx = TransferManagerBuilder.standard().
withS3Client(s3client).
build()
def config_path = "/path/to/my/root"
def dir = "key_path"
def f = new File("${config_path}/${dir}/")
// Method 1, upload whole directory in one call
def mfu = tx.uploadDirectory(data_bucket, dir, f, true)
mfu.waitForCompletion() // <-- throws exception, w/o this line, just doesn't upload, but script continues
// Method 2, upload each file separately
def ld
ld = { File file ->
file.listFiles().each { g ->
if (g.isDirectory()) {
ld.call(g)
} else {
def key = g.toString().substring(config_path_length)
def fu = tx.upload(data_bucket, key, g)
def fu = tx.upload(data_bucket, key, g)
fu.waitForCompletion() // <-- throws exception, w/o this line, just doesn't upload, but script continues
}
}
}
ld.call(f)
// Finally Method 3, avoiding TransferManager altogether, and call putObject directly on each file
def ld
ld = { File file ->
file.listFiles().each { g ->
if (g.isDirectory()) {
ld.call(g)
} else {
def key = g.toString().substring(config_path_length)
def fu = tx.upload(data_bucket, key, g)
s3client.putObject(new PutObjectRequest(data_bucket, key, g)) // <-- throws exception
}
}
}
ld.call(f)
讀但是,不管我嘗試哪一種方法,我總是得到以下堆棧跟蹤的HttpClient和的HttpCore的新版本:
捕獲:java.lang.IllegalStateException:連接池關閉 java.lang.IllegalStateException:連接池在 關閉org.apache.http.util.Asserts.check(Asserts.java:34)在 org.apache。 http.pool.AbstractConnPool.lease(AbstractConnPool.java:184) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection(PoolingHttpClientConnectionManager.java:251) 在 com.amazonaws.http.conn.ClientConnectionManagerFactory $ Handler.invoke(ClientConnectionManagerFactory.java:76) 在com.amazonaws。 http.conn。$ Proxy11.requestConnection(來源不明) 在 org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:175) 在 org.apache.http.impl.execchain.ProtocolExec。執行(ProtocolExec.java:184) 在 org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) 在 org.apache.http.impl.client.CloseableHttpClient.execut E(CloseableHttpClient.java:82) 在 org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) 在 com.amazonaws.http.apache.client.impl.SdkHttpClient.execute( SdkHttpClient.java:72) 在 com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeOneRequest(AmazonHttpClient.java:1190) 在 com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeHelper(AmazonHttpClient.java:1030) 在 com.amazonaws.http.AmazonHttpClient $ RequestExecutor.doExecute(AmazonHttpClient.java:742) 在 com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeWithTimer(AmazonHttpClient.java:716) 在 com.amazonaws.http.AmazonHttpClient $ RequestExecutor.execute(AmazonHttpClient.java:699) 在 com.amazonaws.http.AmazonHttpClient $ RequestExecutor.access $ 500(AmazonHttpClient.java:667) 在 com.amazonaws。 http.AmazonHttpClient $ RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649) 在 com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513) 在 com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client。 java:4221) at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4168) at com.amazonaws.services.s3.AmazonS3Client.putO bject(AmazonS3Client.java:1718) at com.amazonaws.services.s3.AmazonS3 $ putObject.call(Unknown Source) ...
我目前無法確認groovy是否使用更新的httpcomponent庫,或者如果這是問題所在。安裝在1.8 JDK中的有httpclient_4.2.6和httpclient_4.3.5。任何關於如何取消粘貼的建議將不勝感激。謝謝。 -Vincent
你是如何解決這個問題的?我也得到這個異常? –
@shanelee我停止傳遞TransferManager參考,而是在每個傳遞s3文件的函數中使用一個新參數。 –