|
| 1 | +"""An Upload is raw data to be transformed into a Scene""" |
| 2 | +import glob |
| 3 | +import os |
| 4 | + |
| 5 | +import boto3 |
| 6 | + |
| 7 | + |
| 8 | +class Upload(object): |
| 9 | + """A Raster Foundry upload""" |
| 10 | + |
| 11 | + s3_client = boto3.client('s3') |
| 12 | + |
| 13 | + def __repr__(self): |
| 14 | + return '<Upload - {}>'.format(self.name) |
| 15 | + |
| 16 | + def __init__(self, upload, api): |
| 17 | + """Instantiate a new Upload |
| 18 | +
|
| 19 | + Args: |
| 20 | + upload (Upload): generated Upload object from specification |
| 21 | + api (API): api used to make requests on behalf of an upload |
| 22 | + """ |
| 23 | + |
| 24 | + self._upload = upload |
| 25 | + self.api = api |
| 26 | + |
| 27 | + self.id = upload.id |
| 28 | + self.upload_type = upload.uploadType |
| 29 | + self.metadata = upload.metadata |
| 30 | + self.files = upload.files |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def upload_create_from_files( |
| 34 | + cls, datasource, organization, paths_to_tifs, |
| 35 | + dest_bucket, dest_prefix, metadata={}, visibility='PRIVATE', |
| 36 | + project_id=None, dry_run=False |
| 37 | + ): |
| 38 | + """Create an Upload from a set of tifs |
| 39 | +
|
| 40 | + Args: |
| 41 | + datasource (str): UUID of the datasource this upload belongs to |
| 42 | + organization (str): UUID of the organization this upload belongs to |
| 43 | + paths_to_tifs (str | str[]): which tifs to upload. If passed a |
| 44 | + string, files will be the unix path expansion of the passed |
| 45 | + string, e.g., '*.tif' will become an array of all of the tifs |
| 46 | + in the current working directory. If passed a list, files will |
| 47 | + be exactly those files in the list. |
| 48 | + dest_bucket (str): s3 bucket to upload local files to. If the |
| 49 | + Raster Foundry application does not have permission to read |
| 50 | + from this location, the upload will fail to process. |
| 51 | + dest_prefix (str): s3 prefix to upload local files to. If the |
| 52 | + Raster Foundry application does not have permission to read |
| 53 | + from this location, the upload will fail to process. |
| 54 | + metadata (dict): Additional information to store with this upload. |
| 55 | + acquisitionDate and cloudCover will be parsed into any created |
| 56 | + scenes. |
| 57 | + visibility (str): PUBLIC, PRIVATE, or ORGANIZATION visibility level |
| 58 | + for the created scenes |
| 59 | + project_id (str): UUID of the project scenes from this upload |
| 60 | + should be added to |
| 61 | + dry_run (bool): whether to perform side-effecting actions like |
| 62 | + uploads to s3 |
| 63 | +
|
| 64 | + Returns: |
| 65 | + dict: splattable object to post to /uploads/ |
| 66 | + """ |
| 67 | + if isinstance(paths_to_tifs, str): |
| 68 | + paths = glob.glob(paths_to_tifs) |
| 69 | + else: |
| 70 | + paths = paths_to_tifs |
| 71 | + upload_status = 'UPLOADED' |
| 72 | + file_type = 'GEOTIFF' |
| 73 | + |
| 74 | + files = [] |
| 75 | + for f in paths: |
| 76 | + fname = os.path.split(f)[-1] |
| 77 | + key = '/'.join([x for x in [dest_prefix, fname] if x]) |
| 78 | + dest_path = 's3://' + '/'.join( |
| 79 | + [x for x in [dest_bucket, dest_prefix, fname] if x] |
| 80 | + ) |
| 81 | + if not dry_run: |
| 82 | + with open(f, 'r') as inf: |
| 83 | + cls.s3_client.put_object( |
| 84 | + Body=inf.read(), |
| 85 | + Bucket=dest_bucket, |
| 86 | + Key=key |
| 87 | + ) |
| 88 | + files.append(dest_path) |
| 89 | + |
| 90 | + return dict( |
| 91 | + uploadStatus=upload_status, |
| 92 | + files=files, |
| 93 | + uploadType='S3', |
| 94 | + fileType=file_type, |
| 95 | + datasource=datasource, |
| 96 | + organizationId=organization, |
| 97 | + metadata=metadata, |
| 98 | + visibility=visibility, |
| 99 | + projectId=project_id |
| 100 | + ) |
| 101 | + |
| 102 | + @classmethod |
| 103 | + def create(cls, api, upload_create): |
| 104 | + """Post an upload to Raster Foundry for processing |
| 105 | +
|
| 106 | + Args: |
| 107 | + api (API): API to use for requests |
| 108 | + upload_create (dict): post parameters for /uploads. See |
| 109 | + upload_create_from_files |
| 110 | +
|
| 111 | + Returns: |
| 112 | + Upload: created object in Raster Foundry |
| 113 | + """ |
| 114 | + |
| 115 | + return api.client.Imagery.post_uploads(Upload=upload_create).result() |
0 commit comments