Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
indicators-parser-saas
backend
Compare revisions
ca7a59737b2ea6273c066e2ca327eb468cfcf60c...23931598fa077cf2d53134a8884daa747fea23d3
Source
indicators-parser-saas/backend
Select target project
23931598fa077cf2d53134a8884daa747fea23d3
Select Git revision
..
..
...
Target
indicators-parser-saas/backend
Select target project
indicators-parser-saas/backend
ca7a59737b2ea6273c066e2ca327eb468cfcf60c
Select Git revision
Compare
Swap revisions
Commits (2)
Created S3 save\delete\generate link methods
· 173d12e1
Власов Иван Юрьевич
authored
2 years ago
173d12e1
Created S3 load\remove\generate url tests
· 23931598
Власов Иван Юрьевич
authored
2 years ago
23931598
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
integrations/s3.py
+58
-0
integrations/s3.py
settings.py
+8
-0
settings.py
tests/test_s3.py
+39
-0
tests/test_s3.py
with
105 additions
and
0 deletions
+105
-0
integrations/s3.py
0 → 100644
View file @
23931598
import
boto3
as
boto3
from
settings
import
REPORTS_FOLDER
,
ACCESS_KEY
,
SECRET_KEY
,
ENDPOINT_URL
,
REGION_NAME
,
BUCKET_NAME
s3_client
=
boto3
.
client
(
"s3"
,
aws_access_key_id
=
ACCESS_KEY
,
aws_secret_access_key
=
SECRET_KEY
,
endpoint_url
=
ENDPOINT_URL
,
region_name
=
REGION_NAME
,
)
def
save_report_to_s3
(
file
,
report_id
,
is_public
=
False
):
"""
Saves the report to S3
Args:
file: Report file
report_id: Report ID
is_public: Flag, shows, might this report and all indicators from it become public
"""
file_path
=
f
"
{
REPORTS_FOLDER
}
/
{
report_id
}
.pdf"
s3_client
.
upload_file
(
file
,
BUCKET_NAME
,
file_path
)
if
is_public
:
s3_client
.
put_object_acl
(
ACL
=
"public-read"
,
Bucket
=
BUCKET_NAME
,
Key
=
file_path
)
return
file_path
def
remove_report_from_s3
(
report_id
):
"""
Removes the report from S3
Args:
report_id: Report ID
"""
file_path
=
f
"
{
REPORTS_FOLDER
}
/
{
report_id
}
.pdf"
s3_client
.
delete_object
(
Bucket
=
BUCKET_NAME
,
Key
=
file_path
)
return
file_path
def
generate_report_url
(
report_id
):
"""
Generates the report URL
Args:
report_id: Report ID
"""
file_path
=
f
"
{
REPORTS_FOLDER
}
/
{
report_id
}
.pdf"
url
=
s3_client
.
generate_presigned_url
(
"get_object"
,
Params
=
{
"Bucket"
:
BUCKET_NAME
,
"Key"
:
file_path
,
},
ExpiresIn
=
3600
*
24
)
return
url
This diff is collapsed.
Click to expand it.
settings.py
View file @
23931598
...
...
@@ -12,3 +12,11 @@ GITHUB_CLIENT_ID = getenv("GITHUB_CLIENT_ID")
GITHUB_CLIENT_SECRET
=
getenv
(
"GITHUB_CLIENT_SECRET"
)
JWK_KEY_FILE
=
getenv
(
"JWK_KEY_FILE"
)
if
getenv
(
"JWK_KEY_FILE"
)
is
not
None
else
"key.jwk"
SECRET_KEY
=
getenv
(
"S3_SECRET_KEY"
)
ACCESS_KEY
=
getenv
(
"S3_ACCESS_KEY"
)
BUCKET_NAME
=
getenv
(
"S3_BUCKET_NAME"
)
REGION_NAME
=
getenv
(
"S3_REGION_NAME"
)
ENDPOINT_URL
=
getenv
(
"S3_ENDPOINT_URL"
)
REPORTS_FOLDER
=
getenv
(
"S3_REPORTS_FOLDER"
)
if
getenv
(
"S3_REPORTS_FOLDER"
)
is
not
None
else
"reports"
S3_BASE_URL
=
getenv
(
"S3_BASE_URL"
)
This diff is collapsed.
Click to expand it.
tests/test_s3.py
0 → 100644
View file @
23931598
import
os
from
requests
import
get
from
integrations.s3
import
save_report_to_s3
,
remove_report_from_s3
,
generate_report_url
from
settings
import
S3_BASE_URL
def
test_s3_file_load_public
():
pdf_url
=
"https://storage.yandexcloud.net/ivanprogramming/Network_Report.pdf"
pdf_path
=
"Network_Report.pdf"
with
open
(
pdf_path
,
"wb"
)
as
f
:
f
.
write
(
get
(
pdf_url
).
content
)
save_report_to_s3
(
pdf_path
,
"test"
,
is_public
=
True
)
url
=
S3_BASE_URL
+
"test.pdf"
response
=
get
(
url
)
assert
response
.
status_code
==
200
remove_report_from_s3
(
"test"
)
os
.
remove
(
pdf_path
)
def
test_s3_file_load_private
():
pdf_url
=
"https://storage.yandexcloud.net/ivanprogramming/Network_Report.pdf"
pdf_path
=
"Network_Report.pdf"
with
open
(
pdf_path
,
"wb"
)
as
f
:
f
.
write
(
get
(
pdf_url
).
content
)
save_report_to_s3
(
pdf_path
,
"test-private"
,
is_public
=
False
)
url
=
S3_BASE_URL
+
"test.pdf"
response
=
get
(
url
)
assert
response
.
status_code
==
403
presigned_url
=
generate_report_url
(
"test-private"
)
response
=
get
(
presigned_url
)
assert
response
.
status_code
==
200
remove_report_from_s3
(
"test-private"
)
os
.
remove
(
pdf_path
)
This diff is collapsed.
Click to expand it.
Menu
Explore
Projects
Groups
Topics
Snippets