rcai commited on
Commit
908cd86
·
verified ·
1 Parent(s): cd081ac

Update test.py

Browse files
Files changed (1) hide show
  1. test.py +60 -0
test.py CHANGED
@@ -5,6 +5,66 @@ import json
5
  import pandas as pd
6
  import re
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # List containing data from the snapshot
9
  data = [
10
  ['p.G6bS$ts', 'p.G6bS$ts | 8', '8 |', 'C.1994delG', 'p.G6bS$ts | 17'],
 
5
  import pandas as pd
6
  import re
7
 
8
+ ----
9
+ import boto3
10
+ import botocore
11
+
12
+ def list_files_in_bucket(bucket_name, prefix=''):
13
+ """
14
+ List all files in a given S3 bucket.
15
+
16
+ Parameters:
17
+ - bucket_name (str): The name of the S3 bucket.
18
+ - prefix (str): The prefix to filter files (useful for listing files in a specific folder).
19
+
20
+ Returns:
21
+ - list: A list of file keys in the bucket.
22
+ """
23
+ s3_client = boto3.client('s3')
24
+ try:
25
+ response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
26
+ files = [content['Key'] for content in response.get('Contents', [])]
27
+ return files
28
+ except botocore.exceptions.ClientError as e:
29
+ print(f"Error listing files: {e}")
30
+ return []
31
+
32
+ def read_file_from_s3(bucket_name, file_key):
33
+ """
34
+ Read the contents of a file in an S3 bucket.
35
+
36
+ Parameters:
37
+ - bucket_name (str): The name of the S3 bucket.
38
+ - file_key (str): The key of the file in the S3 bucket.
39
+
40
+ Returns:
41
+ - str: The content of the file as a string.
42
+ """
43
+ s3_client = boto3.client('s3')
44
+ try:
45
+ obj = s3_client.get_object(Bucket=bucket_name, Key=file_key)
46
+ content = obj['Body'].read().decode('utf-8')
47
+ return content
48
+ except botocore.exceptions.ClientError as e:
49
+ print(f"Error reading file: {e}")
50
+ return None
51
+
52
+ # Example usage
53
+ bucket_name = 'your-bucket-name'
54
+ prefix = 'your/prefix/' # Optional, if you want to list files in a specific folder
55
+
56
+ # List files in the bucket
57
+ files = list_files_in_bucket(bucket_name, prefix)
58
+ print(f"Files in bucket '{bucket_name}': {files}")
59
+
60
+ # Read the content of the first file (if it exists)
61
+ if files:
62
+ file_key = files[0]
63
+ content = read_file_from_s3(bucket_name, file_key)
64
+ print(f"Content of '{file_key}':\n{content}")
65
+
66
+ -----
67
+
68
  # List containing data from the snapshot
69
  data = [
70
  ['p.G6bS$ts', 'p.G6bS$ts | 8', '8 |', 'C.1994delG', 'p.G6bS$ts | 17'],