I’m calling the YouTube API to download and store channel statistics in S3. I can write a csv file to my S3 bucket without any errors, but it’s empty. I have checked this thread Why the csv file in S3 is empty after loading from Lambda, but I’m not using a with
block in to_csv_channel()
.
I’m currently running the script locally with Windows task scheduler with a slightly modified to_csv_channel()
method that appends data to an existing csv, but in this case I would like to write a new csv file to S3 every day.
JavaScript
x
60
60
1
import json
2
import requests
3
from datetime import date
4
from datetime import datetime
5
import csv
6
import boto3
7
8
class YTstats:
9
10
def __init__(self, api_key, channel_id):
11
self.api_key = api_key
12
self.channel_id = channel_id
13
self.channel_name = None
14
self.channel_statistics = None
15
16
def get_channel_title(self):
17
"""Get the channel name."""
18
19
url_title = f'https://www.googleapis.com/youtube/v3/channels?part=snippet&id={self.channel_id}&key={self.api_key}'
20
21
json_url_title = requests.get(url_title)
22
channel_name_json = json.loads(json_url_title.text)
23
channel_name_json = channel_name_json['items'][0]['snippet']['title']
24
25
self.channel_name = channel_name_json
26
return channel_name_json
27
28
def get_channel_statistics(self):
29
"""Extract channel statistics"""
30
31
url = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&id={self.channel_id}&key={self.api_key}'
32
33
json_url = requests.get(url)
34
statistics = json.loads(json_url.text)
35
statistics = statistics['items'][0]
36
37
self.channel_statistics = statistics
38
return statistics
39
40
def to_csv_channel(self):
41
"""Saves the channel statistics to a csv file."""
42
43
s3 = boto3.client('s3')
44
45
date_col = datetime.now()
46
dt = datetime.strftime(date_col, '%Y-%m-%d')
47
48
self.get_channel_title()
49
channel = self.channel_name
50
channel_id = self.channel_statistics['id']
51
views = self.channel_statistics['statistics']['viewCount']
52
53
temp_csv_file = csv.writer(open('/tmp/youtube.csv', 'w'))
54
temp_csv_file.writerow(['channel_id', 'channel', 'views']) # column headers
55
temp_csv_file.writerow({'channel_id': channel_id, 'channel': channel, 'views': views}) # rows
56
57
final_file_name='youtube-api/youtube_'+ dt +'.csv'
58
59
s3.upload_file('/tmp/youtube.csv', Bucket = 'my-aws-bucket', Key = final_file_name)
60
My lambda_handler:
JavaScript
1
18
18
1
from youtube import YTstats
2
3
def lambda_handler(event, context):
4
5
key = 'my_youtube_api_key'
6
7
# Mike Shake, Studson Studio
8
channel_ids = ['UC6ktP3PLU5sAJxN9Rb0TALg','UC6u6uY4VbvuNtU0BU7F9olw']
9
10
for id in channel_ids:
11
yt = YTstats(key, id)
12
yt.get_channel_statistics()
13
yt.to_csv_channel()
14
15
return {
16
'statusCode': 200
17
}
18
Any help would be appreciated.
Advertisement
Answer
JavaScript
1
11
11
1
import boto3
2
3
def lambda_handler(event, context):
4
bucket_name = 'mybucketname'
5
object_key = 'folder/sub/path/to/s3key'
6
s3 = boto3.resource('s3')
7
file = open("/tmp/myfile.txt", "w")
8
file.write("Your text goes here")
9
file.close()
10
s3.Bucket(bucket_name).upload_file('/tmp/myfile.txt', object_key)
11
similar post : How to write a file or data to an S3 object using boto3