A class for uploading log files from tests to Amazon S3. Those files can then be shared easily.
| 4 | |
| 5 | |
| 6 | class S3LoggingBucket(object): |
| 7 | """A class for uploading log files from tests to Amazon S3. |
| 8 | Those files can then be shared easily.""" |
| 9 | from seleniumbase.config import settings |
| 10 | |
| 11 | def __init__( |
| 12 | self, |
| 13 | log_bucket=settings.S3_LOG_BUCKET, |
| 14 | bucket_url=settings.S3_BUCKET_URL, |
| 15 | selenium_access_key=settings.S3_SELENIUM_ACCESS_KEY, |
| 16 | selenium_secret_key=settings.S3_SELENIUM_SECRET_KEY, |
| 17 | ): |
| 18 | import fasteners |
| 19 | from seleniumbase.fixtures import constants |
| 20 | from seleniumbase.fixtures import shared_utils |
| 21 | |
| 22 | pip_find_lock = fasteners.InterProcessLock( |
| 23 | constants.PipInstall.FINDLOCK |
| 24 | ) |
| 25 | with pip_find_lock: |
| 26 | try: |
| 27 | import boto3 |
| 28 | except Exception: |
| 29 | shared_utils.pip_install("boto3") |
| 30 | import boto3 |
| 31 | self.conn = boto3.Session( |
| 32 | aws_access_key_id=selenium_access_key, |
| 33 | aws_secret_access_key=selenium_secret_key, |
| 34 | ) |
| 35 | self.bucket = log_bucket |
| 36 | self.bucket_url = bucket_url |
| 37 | |
| 38 | def get_key(self, file_name): |
| 39 | """Create a new S3 connection instance with the given name.""" |
| 40 | return self.conn.resource("s3").Object(self.bucket, file_name) |
| 41 | |
| 42 | def get_bucket(self): |
| 43 | """Return the bucket being used.""" |
| 44 | return self.bucket |
| 45 | |
| 46 | def upload_file(self, file_name, file_path): |
| 47 | """Upload a given file from the file_path to the bucket |
| 48 | with the new name/path file_name.""" |
| 49 | upload_key = self.get_key(file_name) |
| 50 | content_type = "text/plain" |
| 51 | if file_name.endswith(".html"): |
| 52 | content_type = "text/html" |
| 53 | elif file_name.endswith(".jpg"): |
| 54 | content_type = "image/jpeg" |
| 55 | elif file_name.endswith(".png"): |
| 56 | content_type = "image/png" |
| 57 | upload_key.Bucket().upload_file( |
| 58 | file_path, |
| 59 | file_name, |
| 60 | ExtraArgs={"ACL": "public-read", "ContentType": content_type}, |
| 61 | ) |
| 62 | |
| 63 | def upload_index_file( |