Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
1,491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Author Ondrej Barta
  4. # ondrej@ondrej.it
  5. # Copyright 2017
  6.  
  7. import os
  8. from urllib import unquote
  9. from urlparse import urlparse
  10.  
  11. import boto3
  12. from botocore.client import Config
  13. from celery.decorators import task
  14.  
  15.  
  16. @task(name="s3.move", queue="mcp")
  17. def move(from_url, to_url):
  18. """
  19. Presune objekt v s3.
  20. Access a key musi byt zaqoutovany.
  21. :param from_url: s3://access:key@bucket/key
  22. :param to_url: s3://bucket/key
  23. :return: None
  24. """
  25. from_parse = urlparse(from_url)
  26. to_parse = unquote(to_url)
  27.  
  28. username = unquote(from_parse.username)
  29. password = unquote(from_parse.password)
  30. from_bucket = from_parse.hostname
  31. from_key = from_parse.path.lstrip("/")
  32.  
  33. to_bucket = to_parse.hostname
  34. to_key = to_parse.path.lstrip("/")
  35.  
  36. s3 = boto3.client(
  37. "s3",
  38. # Nektere regiony podporuji jen s3v4
  39. config=Config(signature_version="s3v4"),
  40. aws_access_key_id=username,
  41. aws_secret_access_key=password,
  42. region_name=os.environ.get("AWS_REGION", "eu-west-1"),
  43. )
  44. # FIXME do budoucna chceme nastavovat acl atp pomoci #...
  45. s3.copy_object(
  46. Bucket=to_bucket,
  47. Key=to_key,
  48. ACL="private",
  49. StorageClass=os.environ.get("AWS_STORAGE_CLASS", "STANDARD_IA"),
  50. CopySource={"Bucket": from_key, "Key": from_key},
  51. )
  52. s3.delete_object(Bucket=from_bucket, Key=from_key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement