def test_copy_nonexistent(self): src_key = self.test_path + 'not_a_real_file_does_not_exist' dest_key = self.test_path + 'destination_file_location' request = messages.CopyRequest(self.test_bucket, src_key, self.test_bucket, dest_key) with self.assertRaises(messages.S3ClientError) as e: self.client.copy(request) self.assertEqual(e.exception.code, 404)
def copy(self, src, dest): """Copies a single S3 file object from src to dest. Args: src: S3 file path pattern in the form s3://<bucket>/<name>. dest: S3 file path pattern in the form s3://<bucket>/<name>. Raises: TimeoutError: on timeout. """ src_bucket, src_key = parse_s3_path(src) dest_bucket, dest_key = parse_s3_path(dest) request = messages.CopyRequest(src_bucket, src_key, dest_bucket, dest_key) self.client.copy(request)
def copy_paths(self, src_dest_pairs): """Copies the given S3 objects from src to dest. This can handle directory or file paths. Args: src_dest_pairs: list of (src, dest) tuples of s3://<bucket>/<name> file paths to copy from src to dest Returns: List of tuples of (src, dest, exception) in the same order as the src_dest_pairs argument, where exception is None if the operation succeeded or the relevant exception if the operation failed. """ if not src_dest_pairs: return [] results = [] for src_path, dest_path in src_dest_pairs: # Copy a directory with self.copy_tree if src_path.endswith('/') and dest_path.endswith('/'): try: results += self.copy_tree(src_path, dest_path) except messages.S3ClientError as err: results.append((src_path, dest_path, err)) # Copy individual files with self.copy elif not src_path.endswith('/') and not dest_path.endswith('/'): src_bucket, src_key = parse_s3_path(src_path) dest_bucket, dest_key = parse_s3_path(dest_path) request = messages.CopyRequest(src_bucket, src_key, dest_bucket, dest_key) try: self.client.copy(request) results.append((src_path, dest_path, None)) except messages.S3ClientError as e: results.append((src_path, dest_path, e)) # Mismatched paths (one directory, one non-directory) get an error result else: err = messages.S3ClientError( "Can't copy mismatched paths (one directory, one non-directory):" + ' %s, %s' % (src_path, dest_path), 400) results.append((src_path, dest_path, err)) return results