Upload a file to diffgram from an S3 bucket :param s3_file_key: path of file to fetch from :return: file obj if file was uploaded, else False
(self, opts)
| 93 | |
| 94 | @with_connection |
| 95 | def __fetch_object(self, opts): |
| 96 | """Upload a file to diffgram from an S3 bucket |
| 97 | |
| 98 | :param s3_file_key: path of file to fetch from |
| 99 | :return: file obj if file was uploaded, else False |
| 100 | """ |
| 101 | spec_list = [{'bucket_name': str, 'path': str}] |
| 102 | log = regular_log.default() |
| 103 | log, input = regular_input.input_check_many(untrusted_input = opts, |
| 104 | spec_list = spec_list, |
| 105 | log = log) |
| 106 | if len(log["error"].keys()) >= 1: |
| 107 | return {'log': log} |
| 108 | # This might be an issue. Currently not supporting urls with no expiration. Biggest time is 1 week. |
| 109 | signed_url = self.connection_client.generate_presigned_url('get_object', |
| 110 | Params = {'Bucket': opts['bucket_name'], |
| 111 | 'Key': opts['path']}, |
| 112 | ExpiresIn = 3600 * 24 * 6) # 5 Days. |
| 113 | |
| 114 | with sessionMaker.session_scope() as session: |
| 115 | |
| 116 | project = Project.get_by_string_id(session, self.config_data.get('project_string_id')) |
| 117 | member = session.query(Member).filter(Member.user_id == opts['event_data']['request_user']).first() |
| 118 | # Deduct Media Type: |
| 119 | extension = Path(opts['path']).suffix |
| 120 | extension = extension.lower() |
| 121 | media_type = None |
| 122 | if extension in images_allowed_file_names: |
| 123 | media_type = 'image' |
| 124 | elif extension in videos_allowed_file_names: |
| 125 | media_type = 'video' |
| 126 | else: |
| 127 | # TODO: Decide, do we want to raise an exception? or just do nothing? |
| 128 | log = regular_log.default() |
| 129 | log['error']['invalid_type'] = 'File must type of: {} {}'.format(str(images_allowed_file_names), |
| 130 | str(videos_allowed_file_names)) |
| 131 | log['error']['file_name'] = opts['path'] |
| 132 | log['opts'] = opts |
| 133 | Event.new( |
| 134 | session = session, |
| 135 | member_id = opts['event_data']['request_user'], |
| 136 | kind = 'aws_s3_new_import_warning', |
| 137 | description = f"Skipped import for {opts['path']}, invalid file type.", |
| 138 | error_log = log, |
| 139 | project_id = project.id, |
| 140 | member = member, |
| 141 | success = False |
| 142 | ) |
| 143 | return None |
| 144 | |
| 145 | # metadata = self.connection_client.head_object(Bucket=self.config_data['bucket_name'], Key=path) |
| 146 | member = session.query(Member).filter(Member.user_id == opts['event_data']['request_user']).first() |
| 147 | created_input = packet.enqueue_packet(self.config_data['project_string_id'], |
| 148 | session = session, |
| 149 | media_url = signed_url, |
| 150 | media_type = media_type, |
| 151 | file_name = opts['path'], |
| 152 | job_id = opts.get('job_id'), |
no test coverage detected