-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
31 lines (25 loc) · 1003 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
import os
class YaUploader:
def __init__(self, token: str):
self.token = token
def get_headers(self):
return {
'Content-Type': 'application/json',
'Authorization': 'OAuth {}'.format(self.token)
}
def upload(self, file_path: str):
upload_url = "https://cloud-api.yandex.net/v1/disk/resources/upload"
headers = self.get_headers()
file_name = os.path.basename(file_path)
params = {"path": file_name, "overwrite": "true"}
href = requests.get(upload_url, headers=headers, params=params).json()['href']
response = requests.put(href, data=open(file_name, 'rb'))
response.raise_for_status()
if response.status_code == 201:
print('Файл загружен')
if __name__ == '__main__':
path_to_file = 'C:/Users/Sergey/PycharmProjects/pythonProject5/data.txt'
token = ''
uploader = YaUploader(token)
result = uploader.upload(path_to_file)