set policy

This commit is contained in:
Page Asgardius 2023-01-22 06:11:28 -07:00
parent 16fca27422
commit 0fe7f7a79b
5 changed files with 117 additions and 25 deletions

View file

@ -2,6 +2,7 @@ package asgardius.page.s3manager;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -24,6 +25,7 @@ public class BucketPolicy extends AppCompatActivity {
boolean style; boolean style;
ProgressBar simpleProgressBar; ProgressBar simpleProgressBar;
TextView permission; TextView permission;
Button setpublic, setprivate;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -38,6 +40,7 @@ public class BucketPolicy extends AppCompatActivity {
bucket = getIntent().getStringExtra("bucket"); bucket = getIntent().getStringExtra("bucket");
style = getIntent().getBooleanExtra("style", false); style = getIntent().getBooleanExtra("style", false);
location = getIntent().getStringExtra("region"); location = getIntent().getStringExtra("region");
getSupportActionBar().setTitle(bucket+"/");
region = Region.getRegion(location); region = Region.getRegion(location);
s3ClientOptions = S3ClientOptions.builder().build(); s3ClientOptions = S3ClientOptions.builder().build();
myCredentials = new BasicAWSCredentials(username, password); myCredentials = new BasicAWSCredentials(username, password);
@ -49,6 +52,8 @@ public class BucketPolicy extends AppCompatActivity {
s3client.setEndpoint(endpoint); s3client.setEndpoint(endpoint);
s3ClientOptions.setPathStyleAccess(style); s3ClientOptions.setPathStyleAccess(style);
s3client.setS3ClientOptions(s3ClientOptions); s3client.setS3ClientOptions(s3ClientOptions);
setprivate = (Button)findViewById(R.id.set_private);
setpublic = (Button)findViewById(R.id.set_public);
Thread getPolicy = new Thread(new Runnable() { Thread getPolicy = new Thread(new Runnable() {
@Override @Override
@ -67,7 +72,9 @@ public class BucketPolicy extends AppCompatActivity {
permission.setText(getResources().getString(R.string.public_bucket)); permission.setText(getResources().getString(R.string.public_bucket));
} else { } else {
permission.setText(getResources().getString(R.string.custom_policy)); permission.setText(getResources().getString(R.string.custom_policy));
setpublic.setVisibility(View.VISIBLE);
} }
setprivate.setVisibility(View.VISIBLE);
simpleProgressBar.setVisibility(View.INVISIBLE); simpleProgressBar.setVisibility(View.INVISIBLE);
} }
}); });
@ -79,8 +86,9 @@ public class BucketPolicy extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
if(e.toString().contains("NoSuchBucketPolicy")) { if(e.toString().contains("NoSuchBucketPolicy")) {
simpleProgressBar.setVisibility(View.INVISIBLE);
permission.setText(getResources().getString(R.string.private_bucket)); permission.setText(getResources().getString(R.string.private_bucket));
setpublic.setVisibility(View.VISIBLE);
simpleProgressBar.setVisibility(View.INVISIBLE);
} }
else { else {
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
@ -93,5 +101,92 @@ public class BucketPolicy extends AppCompatActivity {
} }
}); });
getPolicy.start(); getPolicy.start();
setpublic.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//buttonaction
simpleProgressBar.setVisibility(View.VISIBLE);
setPublic();
}
});
setprivate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//buttonaction
simpleProgressBar.setVisibility(View.VISIBLE);
setPrivate();
}
});
}
private void setPublic() {
Thread setPublic = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::"
+bucket+"\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::"+bucket+"/*\"]}]}";
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();
}
private void setPrivate() {
Thread setPrivate = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
s3client.deleteBucketPolicy(bucket);
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();
}
}
});
setPrivate.start();
} }
} }

View file

@ -160,7 +160,7 @@ public class CorsConfig extends AppCompatActivity {
}); });
} }
public void setAllowall() { private void setAllowall() {
Thread addAll = new Thread(new Runnable() { Thread addAll = new Thread(new Runnable() {
@Override @Override
@ -179,7 +179,7 @@ public class CorsConfig extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}); });
@ -191,7 +191,7 @@ public class CorsConfig extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
simpleProgressBar.setVisibility(View.INVISIBLE); simpleProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); 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(); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
@ -201,7 +201,7 @@ public class CorsConfig extends AppCompatActivity {
addAll.start(); addAll.start();
} }
public void setAllowpdf() { private void setAllowpdf() {
Thread addPdf = new Thread(new Runnable() { Thread addPdf = new Thread(new Runnable() {
@Override @Override
@ -220,7 +220,7 @@ public class CorsConfig extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}); });
@ -232,7 +232,7 @@ public class CorsConfig extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
simpleProgressBar.setVisibility(View.INVISIBLE); simpleProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); 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(); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
@ -242,7 +242,7 @@ public class CorsConfig extends AppCompatActivity {
addPdf.start(); addPdf.start();
} }
public void setDeletecors() { private void setDeletecors() {
Thread delCors = new Thread(new Runnable() { Thread delCors = new Thread(new Runnable() {
@Override @Override
@ -255,7 +255,7 @@ public class CorsConfig extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),getResources().getString(R.string.policy_ok), Toast.LENGTH_SHORT).show();
finish(); finish();
} }
}); });
@ -267,7 +267,7 @@ public class CorsConfig extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
simpleProgressBar.setVisibility(View.INVISIBLE); simpleProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); 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(); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();

View file

@ -24,24 +24,17 @@
android:textSize="25sp"/> android:textSize="25sp"/>
<Button <Button
android:id="@+id/allow_all" android:id="@+id/set_public"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="70dp" android:layout_height="70dp"
android:text="@string/allow_all" android:text="@string/set_public_bucket"
android:visibility="gone" /> android:visibility="gone" />
<Button <Button
android:id="@+id/allow_pdf" android:id="@+id/set_private"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="70dp" android:layout_height="70dp"
android:text="@string/allow_pdf" android:text="@string/set_private_bucket"
android:visibility="gone" />
<Button
android:id="@+id/delete_cors"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="@string/delete_cors"
android:visibility="gone" /> android:visibility="gone" />
</LinearLayout> </LinearLayout>

View file

@ -103,8 +103,8 @@
<string name="allow_all">Permitir todo</string> <string name="allow_all">Permitir todo</string>
<string name="allow_pdf">Permitir pdf</string> <string name="allow_pdf">Permitir pdf</string>
<string name="delete_cors">Eliminar CORS</string> <string name="delete_cors">Eliminar CORS</string>
<string name="cors_error">No se pudo cambiar la política CORS</string> <string name="policy_error">No se pudo cambiar la política</string>
<string name="cors_ok">Política CORS cambiada exitosamente</string> <string name="policy_ok">Política cambiada exitosamente</string>
<string name="download_bucket">Descargar contenido del bucket</string> <string name="download_bucket">Descargar contenido del bucket</string>
<string name="download_folder">Descargar contenido de la carpeta</string> <string name="download_folder">Descargar contenido de la carpeta</string>
<string name="batch_download_success">Archivos descargados exitosamente</string> <string name="batch_download_success">Archivos descargados exitosamente</string>
@ -119,4 +119,6 @@
<string name="public_bucket">Bucket público</string> <string name="public_bucket">Bucket público</string>
<string name="private_bucket">Bucket privado</string> <string name="private_bucket">Bucket privado</string>
<string name="custom_policy">Política personalizada</string> <string name="custom_policy">Política personalizada</string>
<string name="set_public_bucket">Establecer bucket como público</string>
<string name="set_private_bucket">Establecer bucket como privado</string>
</resources> </resources>

View file

@ -110,8 +110,8 @@
<string name="allow_all">Allow all</string> <string name="allow_all">Allow all</string>
<string name="allow_pdf">Allow pdf</string> <string name="allow_pdf">Allow pdf</string>
<string name="delete_cors">Delete CORS</string> <string name="delete_cors">Delete CORS</string>
<string name="cors_error">Cannot change CORS policy</string> <string name="policy_error">Cannot change policy</string>
<string name="cors_ok">CORS policy changed successfully</string> <string name="policy_ok">Policy changed successfully</string>
<string name="copy_name">Copy name to clipboard</string> <string name="copy_name">Copy name to clipboard</string>
<string name="copy_ok">Copied to clipboard</string> <string name="copy_ok">Copied to clipboard</string>
<string name="upload_calculate">Calculating folder size</string> <string name="upload_calculate">Calculating folder size</string>
@ -123,4 +123,6 @@
<string name="public_bucket">Public bucket</string> <string name="public_bucket">Public bucket</string>
<string name="private_bucket">Private bucket</string> <string name="private_bucket">Private bucket</string>
<string name="custom_policy">Private bucket</string> <string name="custom_policy">Private bucket</string>
<string name="set_public_bucket">Set bucket as public</string>
<string name="set_private_bucket">Set bucket as private</string>
</resources> </resources>