我正在开发一款应用程序,该应用程序将使用名为“imdb-datasets”的amazon aws s3 imdb存储桶来显示各种电影信息。在尝试这个之前,我用我自己的桶测试了代码并设法上传并从我的Android设备下载测试文件到我的桶,反之亦然。但是在尝试使用与imdb-datasets存储桶相同的方法时,我开始得到一个拒绝访问错误403.在做了一些研究后我发现可能的问题是我必须在代码中指定它是一个请求者支付存储桶。我试图实现请求者支付位(注释掉的代码),但它崩溃了我的应用程序。我不知道如何在我的应用程序中实现它,因为我没有找到任何指南或解决方案。任何想法如何做到这一点?
编辑:我还通过Android设备设置,通过清单和手动在应用程序中为应用程序提供了访问存储的权限。还分配了与iam用户和imdb存储桶的角色策略。
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import java.io.File;
public class movieChooseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_choose);
}
public void getFile(View v) {
File sdcard = Environment.getExternalStorageDirectory();
String key = "documents/v1/current/title.basics.tsv.gz";
File file = new File(key, "title.basics.tsv.gz");
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new
CognitoCachingCredentialsProvider(
getApplicationContext(),
"eu-west-2:4c829519-0596-4fdb-ad04-6b8912e44164", // Identity
//pool ID
Regions.EU_WEST_2 // Region
);
AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
s3.setRegion(Region.getRegion(Regions.US_EAST_1));
//include the appropriate request-payer parameter
//s3.getObject(new GetObjectRequest("imdb-datasets",
key).withRequesterPays(true));
TransferUtility transferUtility = new TransferUtility(s3,
getApplicationContext());
TransferObserver transferObserver = transferUtility.download("imdb-
datasets","title.basics.tsv.gz",file);
transferObserver.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int i, TransferState state) {
Log.e("statechange",state + " ");
if(state == state.COMPLETED) {
Toast.makeText(movieChooseActivity.this, "File transfered",
Toast.LENGTH_LONG).show();
}
if(state == state.FAILED) {
Toast.makeText(movieChooseActivity.this, "Failed",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onProgressChanged(int i, long l, long l1) {
}
@Override
public void onError(int i, Exception e) {
Log.e("whattowatchapp","Error:"+ e.getMessage());
}
});
}
}
答案 0 :(得分:0)
文件位于documents / v1 / *
下
在您的示例中,密钥只是title.basics.tsv.gz
在他们的代码示例here
中private static String bucketName = "imdb-datasets";
private static String key = "documents/v1/current/name.basics.tsv.gz";
public static void main(String[] args) throws IOException, InterruptedException {
ProfileCredentialsProvider credentialsProvider =
new ProfileCredentialsProvider("*** FULL PATH TO AWS CREDENTIALS FILE ***",
"*** CREDENTIALS PROFILE NAME ***");
AmazonS3 s3Client = new AmazonS3Client(credentialsProvider);
try {
// Note: It's necessary to set RequesterPays to true
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key)
.withRequesterPays(true);
http://www.imdb.com/interfaces/
我不知道该应用的用途或用途,但提醒他们的许可证
您可以保存此数据的本地副本,并受我们的条款和条件约束。请参阅Non-Commercial Licensing和copyright/license并验证合规性。