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
S3: Implement restore object with select job type #7504
Closed
tsugumi-sys
wants to merge
4
commits into
getmoto:master
from
tsugumi-sys:impl-restore-object-for-select-operation
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -48,12 +48,14 @@ | |
InvalidPart, | ||
InvalidPublicAccessBlockConfiguration, | ||
InvalidRequest, | ||
InvalidRestoreRequestType, | ||
InvalidStorageClass, | ||
InvalidTagError, | ||
InvalidTargetBucketForLogging, | ||
MalformedXML, | ||
MissingBucket, | ||
MissingKey, | ||
MissingRequiredParametersForSelectRequest, | ||
NoSuchPublicAccessBlockConfiguration, | ||
NoSuchUpload, | ||
ObjectLockConfigurationNotFoundError, | ||
|
@@ -2885,7 +2887,13 @@ | |
] | ||
|
||
def restore_object( | ||
self, bucket_name: str, key_name: str, days: Optional[str], type_: Optional[str] | ||
self, | ||
bucket_name: str, | ||
key_name: str, | ||
days: Optional[str], | ||
type_: Optional[str], | ||
select_parameters: Optional[Dict[str, Any]], | ||
output_location: Optional[Dict[str, Any]], | ||
) -> bool: | ||
key = self.get_object(bucket_name, key_name) | ||
if not key: | ||
|
@@ -2897,8 +2905,72 @@ | |
if days and type_: | ||
raise DaysMustNotProvidedForSelectRequest() | ||
|
||
if type_ and type_ != "SELECT": | ||
raise InvalidRestoreRequestType() | ||
|
||
if type_ and select_parameters is None: | ||
raise MissingRequiredParametersForSelectRequest("SelectParameters") | ||
|
||
if type_ and output_location is None: | ||
raise MissingRequiredParametersForSelectRequest("OutputLocation") | ||
|
||
if key.storage_class not in ARCHIVE_STORAGE_CLASSES: | ||
raise InvalidObjectState(storage_class=key.storage_class) | ||
|
||
# select request | ||
if type_: | ||
restored_key = copy.deepcopy(key) | ||
output_location_s3 = output_location["S3"] | ||
|
||
# TODO: 'Encryption', 'CannedACL', 'AccessControlList', 'Tagging' and 'UserMetadata' are need to be supported for OutputLocation.S3 configuration. | ||
# set bucket name | ||
_bucket_name = output_location_s3.get("BucketName") | ||
if _bucket_name is not None: | ||
# check if bucket is exists here. | ||
bucket = self.get_bucket(_bucket_name) | ||
else: | ||
bucket = self.bucket(key.bucket_name) | ||
restored_key.bucket_name = bucket.name | ||
|
||
# set key name | ||
_key_name = output_location_s3.get("Prefix") | ||
if _key_name is not None: | ||
restored_key.name = _key_name | ||
key_name = _key_name | ||
else: | ||
key_name = key.name | ||
|
||
# set storage class | ||
_storage = output_location_s3.get("StorageClass") | ||
if _storage is not None: | ||
restored_key.set_storage_class(_storage) | ||
|
||
# set value | ||
contents = self.select_object_content( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
key.bucket_name, | ||
key.name, | ||
select_parameters["Expression"], | ||
select_parameters["InputSerialization"], | ||
) | ||
|
||
# serialize selected object | ||
# TODO: Delimiter extraction is also defined in `serialize_select` method in `moto/s3/select_object_content.py`. This part might be extracted into a common method. | ||
delimiter = ( | ||
(select_parameters["OutputSerialization"].get("JSON") or {}).get( | ||
"RecordDelimiter" | ||
) | ||
or "\n" | ||
).encode("utf-8") | ||
payload_bytes = b"" | ||
for data in contents: | ||
payload_bytes += data + delimiter | ||
restored_key.value = payload_bytes | ||
|
||
# save object to bucket | ||
bucket.keys.setlist(key_name, restored_key) | ||
return True | ||
|
||
# restore object request | ||
had_expiry_date = key.expiry_date is not None | ||
if days: | ||
key.restore(int(days)) | ||
|
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
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.
This check should be done as early as possible as well