2017-03-09 3 views
0

내 응용 프로그램에서 내 aws S3 서버 버킷에 액세스하는 데 문제가 있습니다. 버킷 로그 파일에서 요청을 받았지만 액세스가 거부되었습니다. 나는 서버에 대한 모든 권한을 허용하는 정책과 관리 정책을 가지고 있으며 모든 사람을 허용하도록 서버 권한을 설정했지만 여전히 액세스가 거부되었다고 말합니다. 올바른 풀의 ID도 생성되었습니다. 앱 끝에서 버킷을 찾을 수 없다는 메시지가 나타납니다. 서버 영역은 런던입니다.Amazon 웹 서비스 S3 액세스가 거부되었습니다.

나는이 문제에 관해 많은 다른 질문을 읽었지만 아무도 내 문제를 해결하지 못했습니다.

인라인 인증되지 않은 IAM 정책

{ 
"Version": "2012-10-17", 
"Statement": [ 
    { 
     "Sid": "Stmt1488834891000", 
     "Effect": "Allow", 
     "Action": [ 
      "s3:*" 
     ], 
     "Resource": [ 
      "arn:aws:s3:::ascentserver/*" 
     ] 
    } 
] 

}이 정책 시뮬레이터에서 작동하지 않았다 어떤 이유로

주셔서 감사합니다, 나는 양동이와 서비스, didn를 위해 자원을 추가하는 시도 도와주세요.

관리되는 정책은 기본 S3 전체 액세스이며 작동 중임을 시뮬레이션합니다.

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
    { 
     "Effect": "Allow", 
     "Action": "s3:*", 
     "Resource": "*" 
    } 
    ] 
} 

코드가 서버 주소 니펫 음이 그냥 조각이 아니라 전체 코드

[에 있습니다

public void setFileToUpload(){ 

     TransferObserver transferObserver = transferUtility.upload(
       "http://ascentserver.s3.eu-west-2.amazonaws.com",  /* The bucket to upload to */ 
       "TEST.png", /* The key for the uploaded object */ 
       fileToUpload  /* The file where the data to upload exists */ 
     ); 

     transferObserverListener(transferObserver); 
    } 
+1

은 아마도 더 나은 것 사용 중이거나 S3 요청을하는 코드 스 니펫, 사용중인 IAM 정책 및 첨부 할 엔티티와 S3에 액세스 할 때 사용중인 URL 패턴. –

답변

0

당신은 transferUtility.upload() 방법에 대한 ascentserver 대신 http://ascentserver.s3.eu-west-2.amazonaws.com으로 버킷 이름을 제공하고 AmazonS3Client 기본을 올바른 영역을 사용하지 있는지 확인해야합니다 하나는 명시 적으로 종단점을 s3.setEndpoint("s3.eu-west-2.amazonaws.com")으로 설정합니다.

예를 들어

: 당신은 다른 사람은 그것을 호스팅하는 방법을/당신의 앱, AWS SDK의 유형으로, 당신을 도울 수있는 것이 더 많은 정보를 제공 할 수 있다면

CognitoCachingCredentialsProvider credProvider = new CognitoCachingCredentialsProvider(
    getApplicationContext(),  
    "YOUR_COGNITO_POOL_ID",  
    Regions.EU_WEST_2   
); 

AmazonS3 s3 = new AmazonS3Client(credProvider); 
s3.setEndpoint("s3.eu-west-2.amazonaws.com"); 

TransferUtility transferUtility = new TransferUtility(s3, getApplicationContext()); 

TransferObserver transferObserver = transferUtility.upload(
    "ascentserver", 
    "TEST.png", 
    fileToUpload 
); 

. 
. 
. 
+0

이렇게하면 "버킷은 지정된 끝점을 처리해야합니다"라는 오류가 발생하고 서버에 계속 액세스하지 못합니다. –

+0

'AmazonS3Client'가 올바른 영역을 사용하고's3.setEndpoint ("s3.eu-west-2.amazonaws.com")로 명시 적으로 끝점을 설정하여 기본 영역이 아닌 올바른 영역을 사용해야합니다. 내 업데이트 답변을 참조하십시오. –

0

그래서 우리가 사용 된 코드는 다음과 같다 메인]

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 MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

File fileToUpload = new  File("/storage/emulated/0/Pictures/Screenshots/TEST.png"); 
File fileToDownload = new File("/storage/emulated/0/Download/TEST"); 
AmazonS3 s3; 
TransferUtility transferUtility; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    // callback method to call credentialsProvider method 
    credentialsProvider(); 

    // callback method to call the setTransferUtility method 
    setTransferUtility(); 
} 

public void credentialsProvider(){ 

    // Initialize the Amazon Cognito credentials provider 
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
      getApplicationContext(), 
      "eu-west-1:0e33dea3-6075-4ea5-a268-b0c1364f5107", // Identity Pool ID 
      Regions.EU_WEST_1 // Region 
    ); 

    setAmazonS3Client(credentialsProvider); 
} 

public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider){ 

    // Create an S3 client 
    s3 = new AmazonS3Client(credentialsProvider); 

    // Set the region of your S3 bucket 
    s3.setRegion(Region.getRegion(Regions.EU_WEST_1)); 

} 

public void setTransferUtility(){ 

    transferUtility = new TransferUtility(s3, getApplicationContext()); 
} 

public void setFileToUpload(){ 

    TransferObserver transferObserver = transferUtility.upload(
      "http://ascentserver.s3.eu-west-2.amazonaws.com",  /* The bucket to upload to */ 
      "TEST.png", /* The key for the uploaded object */ 
      fileToUpload  /* The file where the data to upload exists */ 
    ); 

    transferObserverListener(transferObserver); 
} 

public void setFileToDownload(){ 

    TransferObserver transferObserver = transferUtility.download(
      "http://ascentserver.s3.eu-west-2.amazonaws.com",  /* The bucket to download from */ 
      "TEST.png", /* The key for the object to download */ 
      fileToDownload  /* The file to download the object to */ 
    ); 

    transferObserverListener(transferObserver); 

} 

public void transferObserverListener(TransferObserver transferObserver){ 

    // listener that provides status of download 
    transferObserver.setTransferListener(new TransferListener(){ 

     @Override 
     public void onStateChanged(int id, TransferState state) { 
      Log.e("statechange", state+""); 
     } 

     @Override 
     public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 
      int percentage = (int) (bytesCurrent/bytesTotal * 100); 
      Log.e("percentage",percentage +""); 
     } 

     @Override 
     public void onError(int id, Exception ex) { 
      Log.e("error","error"); 
     } 

    }); 
} 
} 

[종속성]

compile 'com.amazonaws:aws-android-sdk-core:2.2.13' 
compile 'com.amazonaws:aws-android-sdk-cognito:2.2.13' 
compile 'com.amazonaws:aws-android-sdk-s3:2.2.13' 
compile 'com.amazonaws:aws-android-sdk-ddb:2.2.13' 

[매니페스트]

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

<service 
     android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" 
     android:enabled="true" /> 
관련 문제