set object policy

This commit is contained in:
Page Asgardius 2023-01-22 19:16:09 -07:00
parent 1b1dc39482
commit 31537846da

View file

@ -16,7 +16,7 @@ import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
public class ObjectPolicy extends AppCompatActivity {
String username, password, endpoint, bucket, location, filename, title;
String username, password, endpoint, bucket, location, filename, title, policy;
Region region;
S3ClientOptions s3ClientOptions;
AWSCredentials myCredentials;
@ -60,7 +60,7 @@ public class ObjectPolicy extends AppCompatActivity {
public void run() {
try {
//Your code goes here
String policy = s3client.getBucketPolicy(bucket).getPolicyText();
policy = s3client.getBucketPolicy(bucket).getPolicyText();
publicbucket = policy.contains("arn:aws:s3:::"+bucket+"/*") && policy.contains("s3:GetObject");
publicobject = false;
if(!publicbucket && policy.contains("s3:GetObject")) {
@ -79,7 +79,7 @@ public class ObjectPolicy extends AppCompatActivity {
}
}
}
//System.out.println(policy);
System.out.println(policy);
runOnUiThread(new Runnable() {
@ -87,11 +87,11 @@ public class ObjectPolicy extends AppCompatActivity {
public void run() {
//Your code goes here
if(publicbucket){
permission.setText(getResources().getString(R.string.public_bucket));
setprivate.setVisibility(View.VISIBLE);
permission.setText(getResources().getString(R.string.public_object));
//setprivate.setVisibility(View.VISIBLE);
}else if(publicobject) {
permission.setText(getResources().getString(R.string.public_object));
setprivate.setVisibility(View.VISIBLE);
//setprivate.setVisibility(View.VISIBLE);
}else {
permission.setText(getResources().getString(R.string.private_object));
setpublic.setVisibility(View.VISIBLE);
@ -107,7 +107,7 @@ public class ObjectPolicy extends AppCompatActivity {
@Override
public void run() {
if(e.toString().contains("NoSuchBucketPolicy")) {
permission.setText(getResources().getString(R.string.private_bucket));
permission.setText(getResources().getString(R.string.private_object));
setpublic.setVisibility(View.VISIBLE);
simpleProgressBar.setVisibility(View.INVISIBLE);
}
@ -122,5 +122,67 @@ public class ObjectPolicy extends AppCompatActivity {
}
});
getPolicy.start();
setpublic.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//buttonaction
simpleProgressBar.setVisibility(View.VISIBLE);
setPublic();
}
});
}
private void setPublic() {
Thread setPublic = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
if(policy == null) {
if(filename.endsWith("/")) {
policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::"+bucket+"/"+filename+"*\"]}]}";
} else {
policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::"+bucket+"/"+filename+"\"]}]}";
}
} else if(policy.endsWith("]}]}")) {
if(filename.endsWith("/")) {
policy = policy.substring(0, policy.length()-4)+",\"arn:aws:s3:::"+bucket+"/"+filename+"*\"]}]}";
} else {
policy = policy.substring(0, policy.length()-4)+",\"arn:aws:s3:::"+bucket+"/"+filename+"\"]}]}";
}
} else {
if(filename.endsWith("/")) {
policy = policy.substring(0, policy.length()-3)+",\"arn:aws:s3:::"+bucket+"/"+filename+"*\"}]}";
} else {
policy = policy.substring(0, policy.length()-3)+",\"arn:aws:s3:::"+bucket+"/"+filename+"\"}]}";
}
}
s3client.setBucketPolicy(bucket, policy);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show();
finish();
}
});
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
simpleProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_error), Toast.LENGTH_SHORT).show();
}
});
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
}
}
});
setPublic.start();
}
}