Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to delete GCS objects #268

Merged
merged 4 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions libcloudforensics/providers/gcp/internal/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,17 @@ def ListBucketObjects(self, bucket: str) -> List[Dict[str, Any]]:
request = gcs_objects.list(bucket=bucket)
objects = request.execute() # type: Dict[str, Any]
return objects.get('items', [])

def DeleteObject(self, gcs_path: str) -> None:
"""Deletes an object in a Google Cloud Storage bucket.

Args:
gcs_path (str): Full path to the object (ie: gs://bucket/dir1/dir1/obj)
"""

if not gcs_path.startswith('gs://'):
gcs_path = 'gs://' + gcs_path
bucket, object_path = SplitGcsPath(gcs_path)
gcs_objects = self.GcsApi().objects()
request = gcs_objects.delete(bucket=bucket, object=object_path)
request.execute() # type: Dict[str, Any]
7 changes: 6 additions & 1 deletion tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
'bucketacls': gcp_cli.GetBucketACLs,
'objectmetadata': gcp_cli.GetGCSObjectMetadata,
'listbuckets': gcp_cli.ListBuckets,
'listobjects': gcp_cli.ListBucketObjects
'listobjects': gcp_cli.ListBucketObjects,
'deleteobject': gcp_cli.DeleteObject
}
}

Expand Down Expand Up @@ -351,6 +352,10 @@ def Main() -> None:
args=[
('path', 'Path to bucket.', None),
])
AddParser('gcp', gcp_subparsers, 'deleteobject', 'Deletes a GCS object',
args=[
('path', 'Path to GCS object.', None),
])

if len(sys.argv) == 1:
parser.print_help()
Expand Down
12 changes: 12 additions & 0 deletions tools/gcp_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,15 @@ def ListBucketObjects(args: 'argparse.Namespace') -> None:
logger.info('{0:s} {1:s}b [{2:s}]'.format(
obj.get('id', 'ID not found'), obj.get('size', 'Unknown size'),
obj.get('contentType', 'Unknown Content-Type')))


def DeleteObject(args: 'argparse.Namespace') -> None:
"""Deletes an object in GCS.

Args:
args (argparse.Namespace): Arguments from ArgumentParser.
"""
gcs = gcp_storage.GoogleCloudStorage(args.project)
gcs.DeleteObject(args.path)

print('Object deleted.')