-
Notifications
You must be signed in to change notification settings - Fork 289
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 upload file any size #3883
Merged
Merged
add upload file any size #3883
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e43169e
add upload file any size
jackzhhuang 19fbef1
fix some type styles
jackzhhuang 7a8199f
remove print count meaningless
jackzhhuang 241afdf
add key in upload large file procedure
jackzhhuang 5bdd952
no need to create bucket
jackzhhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
import os | ||
import sys | ||
|
||
PER_UPLOADING = 104857600 ## up to 100 MB per uploading action | ||
|
||
def upload_file(file_name, bucket, key): | ||
object_name = os.path.basename(file_name) | ||
|
||
s3_client = boto3.client('s3') | ||
try: | ||
response = s3_client.upload_file(file_name, bucket, key) | ||
except ClientError as e: | ||
print(str(e)) | ||
return False | ||
return True | ||
|
||
def upload_file_big_file(file_name, bucket, key): | ||
client = boto3.client('s3') | ||
try: | ||
response = client.create_multipart_upload(Bucket = bucket, Key = key) | ||
upload_id = response["UploadId"] | ||
file_obj = open(file_name, "rb") | ||
content = file_obj.read(PER_UPLOADING) | ||
count = 1 | ||
parts = [] | ||
while content: | ||
response = client.upload_part(Body = content, Bucket = bucket, Key = key, PartNumber = count, UploadId = upload_id) | ||
parts.append({'ETag': response["ETag"], 'PartNumber': count}) | ||
count += 1 | ||
content = file_obj.read(PER_UPLOADING) | ||
response = client.complete_multipart_upload(Bucket = bucket, Key = key, MultipartUpload = {'Parts': parts,}, UploadId = upload_id) | ||
print(str(response)) | ||
except ClientError as e: | ||
print(str(e)) | ||
return False | ||
return True | ||
|
||
def upload_file_any_size(file_name, bucket, key): | ||
file_size = os.path.getsize(file_name) | ||
if file_size > PER_UPLOADING: | ||
upload_file_big_file(file_name, bucket, key) | ||
else: | ||
upload_file(file_name, bucket, key) | ||
|
||
def create_bucket(bucket_name, region=None): | ||
try: | ||
if region is None: | ||
s3_client = boto3.client('s3') | ||
s3_client.create_bucket(Bucket = bucket_name) | ||
else: | ||
s3_client = boto3.client('s3', region_name = region) | ||
location = {'LocationConstraint': region} | ||
s3_client.create_bucket(Bucket = bucket_name, | ||
CreateBucketConfiguration=location) | ||
except ClientError as e: | ||
print(str(e)) | ||
return False | ||
return True | ||
|
||
def list_bucket(): | ||
s3 = boto3.client('s3') | ||
response = s3.list_buckets() | ||
print('Existing buckets:') | ||
for bucket in response['Buckets']: | ||
print(f' {bucket["Name"]}') | ||
|
||
def list_bucket(bucket_name, max_key): | ||
s3 = boto3.client('s3') | ||
response = s3.list_objects(Bucket = bucket_name, MaxKeys = max_key) | ||
for item in response['Contents']: | ||
print(f' {item["Key"]}') | ||
|
||
|
||
def main(): | ||
bucket_name = sys.argv[1] | ||
key = sys.argv[2] | ||
test_file = sys.argv[3] | ||
|
||
print(f"bucket_name: {bucket_name}") | ||
print(f"key: {key}") | ||
print(f"test_file: {test_file}") | ||
upload_file_any_size(test_file, bucket_name, key) | ||
# list_bucket(bucket_name, 10) | ||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是三个参数, 20行是2个参数,好像有问题
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我改好了, 现在可以了,你可以试试,key那个参数是后来加的