Best Python code snippet using localstack_python
storage.py
Source:storage.py
...6# TODO make S3 calls non-blocking7def generate_upload_url(8 bucket_name: str, object_name: str, metadata: Dict[str, str]9) -> str:10 return _generate_presigned_url(bucket_name, object_name, "put_object", metadata)11def generate_download_url(bucket_name: str, object_name: str) -> str:12 return _generate_presigned_url(bucket_name, object_name, "get_object")13def _generate_presigned_url(14 bucket_name: str,15 object_name: str,16 client_method: str,17 metadata: Dict[str, str] = None,18 expiration: int = 300,19) -> str:20 params = {"Bucket": bucket_name, "Key": object_name}21 if metadata:22 params["Metadata"] = metadata23 # Invalid signature without configuring addressing_style24 s3 = boto3_client_localhost("s3", config=Config(s3={"addressing_style": "path"}))25 try:26 url = s3.generate_presigned_url(27 client_method, Params=params, ExpiresIn=expiration...
file.py
Source:file.py
...7import aiobotocore8from app.utils import JsonResponse9from app.config import AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_BUCKET_NAME10blueprint = Blueprint("file", url_prefix="/file")11async def _generate_presigned_url(image_path: str):12 session = aiobotocore.get_session()13 async with session.create_client(14 "s3",15 region_name="ap-northeast-2",16 aws_access_key_id=AWS_ACCESS_KEY_ID,17 aws_secret_access_key=AWS_SECRET_ACCESS_KEY,18 ) as client:19 content_type, _ = mimetypes.guess_type(image_path)20 if content_type is None:21 raise Exception("Failed to parse content type from filename")22 url = await client.generate_presigned_url(23 "put_object",24 Params={25 "Bucket": AWS_BUCKET_NAME,26 "Key": image_path,27 "ContentType": content_type,28 },29 ExpiresIn=300, # 5 minutes30 )31 return url32@blueprint.route("/<file_type:string>/create-signed-url", methods=["POST"])33@jwt_required34async def create_sigined_url(request: Request, file_type: str, token: Token):35 app = request.json.get("app")36 filename = request.json.get("filename")37 ext = filename.split(".")[-1]38 new_filename = uuid.uuid4().hex + "." + ext39 image_path = f"{file_type}/{app}/{new_filename}"40 signed_url = await _generate_presigned_url(image_path)41 return JsonResponse(42 {"image_path": f"{image_path}", "signed_url": signed_url}, status=201...
presigning_loader.py
Source:presigning_loader.py
...8from ..aws.bucket import Bucket9def validate(context, url, normalize_url_func=http_loader._normalize_url):10 return _validate(context, url, normalize_url_func)11@return_future12def _generate_presigned_url(context, bucket, key, callback):13 """14 Generates presigned URL15 :param Context context: Thumbor's context16 :param string bucket: Bucket name17 :param string key: Path to get URL for18 :param callable callback: Callback method once done19 """20 Bucket(bucket, context.config.get('TC_AWS_REGION'),21 context.config.get('TC_AWS_ENDPOINT')).get_url(key, callback=callback)22@return_future23def load(context, url, callback):24 """25 Loads image26 :param Context context: Thumbor's context27 :param string url: Path to load28 :param callable callback: Callback method once done29 """30 if _use_http_loader(context, url):31 http_loader.load_sync(context, url, callback, normalize_url_func=http_loader._normalize_url)32 else:33 bucket, key = _get_bucket_and_key(context, url)34 if _validate_bucket(context, bucket):35 def on_url_generated(generated_url):36 def noop(url):37 return url38 http_loader.load_sync(context, generated_url, callback, normalize_url_func=noop)39 _generate_presigned_url(context, bucket, key, on_url_generated)40 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!