Error while using upload_fileobj()
EkanshZetaGlobal opened this issue · comments
Describe the bug
While using upload_fileobj I receive this error:
expires_in = timedelta(seconds=response["expiresIn"])
KeyError: 'expiresIn'
coming from botocore/tokens.py line number 262
Expected Behavior
It should successfully upload the object to designated s3 bucket
Current Behavior
While using upload_fileobj I receive this error:
expires_in = timedelta(seconds=response["expiresIn"])
KeyError: 'expiresIn'
coming from botocore/tokens.py line number 262
Reproduction Steps
import io
import boto3
Create a small in-memory file
file_content = b"Hello, this is a test file content."
in_memory_file = io.BytesIO(file_content)
in_memory_file.seek(0) # Ensure the file pointer is at the start
Create a boto3 session (use your AWS profile)
session = boto3.Session(profile_name=os.environ.get("AWS_SSO_PROFILE"))
s3 = session.client("s3")
s3.upload_fileobj(in_memory_file, bucket_name, s3_key_name)
print(f"File uploaded successfully to {bucket_name}/{s3_key_name}")
Possible Solution
No response
Additional Information/Context
Complete traceback here:
KeyError Traceback (most recent call last)
File ~/git_repos/project-repo/zh_assistants_service/utils/s3.py:100, in upload_file_content_to_s3(file_data, bucket_name, s3_key, endpoint_url)
99 s3 = boto3.client("s3", endpoint_url=endpoint_url)
--> 100 s3.upload_fileobj(in_memory_file, bucket_name, s3_key)
102 except Exception as e:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/boto3/s3/inject.py:642, in upload_fileobj(self, Fileobj, Bucket, Key, ExtraArgs, Callback, Config)
635 future = manager.upload(
636 fileobj=Fileobj,
637 bucket=Bucket,
(...)
640 subscribers=subscribers,
641 )
--> 642 return future.result()
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/s3transfer/futures.py:103, in TransferFuture.result(self)
99 try:
100 # Usually the result() method blocks until the transfer is done,
101 # however if a KeyboardInterrupt is raised we want want to exit
102 # out of this and propagate the exception.
--> 103 return self._coordinator.result()
104 except KeyboardInterrupt as e:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/s3transfer/futures.py:266, in TransferCoordinator.result(self)
265 if self._exception:
--> 266 raise self._exception
267 return self._result
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/s3transfer/tasks.py:139, in Task.call(self)
138 if not self._transfer_coordinator.done():
--> 139 return self._execute_main(kwargs)
140 except Exception as e:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/s3transfer/tasks.py:162, in Task._execute_main(self, kwargs)
160 logger.debug(f"Executing task {self} with kwargs {kwargs_to_display}")
--> 162 return_value = self._main(**kwargs)
163 # If the task is the final task, then set the TransferFuture's
164 # value to the return value from main().
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/s3transfer/upload.py:764, in PutObjectTask._main(self, client, fileobj, bucket, key, extra_args)
763 with fileobj as body:
--> 764 client.put_object(Bucket=bucket, Key=key, Body=body, **extra_args)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/client.py:565, in ClientCreator._create_api_method.._api_call(self, *args, **kwargs)
564 # The "self" in this scope is referring to the BaseClient.
--> 565 return self._make_api_call(operation_name, kwargs)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/client.py:1001, in BaseClient._make_api_call(self, operation_name, api_params)
1000 apply_request_checksum(request_dict)
-> 1001 http, parsed_response = self._make_request(
1002 operation_model, request_dict, request_context
1003 )
1005 self.meta.events.emit(
1006 'after-call.{service_id}.{operation_name}'.format(
1007 service_id=service_id, operation_name=operation_name
(...)
1012 context=request_context,
1013 )
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/client.py:1027, in BaseClient._make_request(self, operation_model, request_dict, request_context)
1026 try:
-> 1027 return self._endpoint.make_request(operation_model, request_dict)
1028 except Exception as e:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/endpoint.py:119, in Endpoint.make_request(self, operation_model, request_dict)
114 logger.debug(
115 "Making request for %s with params: %s",
116 operation_model,
117 request_dict,
118 )
--> 119 return self._send_request(request_dict, operation_model)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/endpoint.py:198, in Endpoint._send_request(self, request_dict, operation_model)
197 self._update_retries_context(context, attempts)
--> 198 request = self.create_request(request_dict, operation_model)
199 success_response, exception = self._get_response(
200 request, operation_model, context
201 )
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/endpoint.py:134, in Endpoint.create_request(self, params, operation_model)
131 event_name = 'request-created.{service_id}.{op_name}'.format(
132 service_id=service_id, op_name=operation_model.name
133 )
--> 134 self._event_emitter.emit(
135 event_name,
136 request=request,
137 operation_name=operation_model.name,
138 )
139 prepared_request = self.prepare_request(request)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/hooks.py:412, in EventAliaser.emit(self, event_name, **kwargs)
411 aliased_event_name = self._alias_event_name(event_name)
--> 412 return self._emitter.emit(aliased_event_name, **kwargs)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/hooks.py:256, in HierarchicalEmitter.emit(self, event_name, **kwargs)
246 """
247 Emit an event by name with arguments passed as keyword args.
248
(...)
254 handlers.
255 """
--> 256 return self._emit(event_name, kwargs)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/hooks.py:239, in HierarchicalEmitter._emit(self, event_name, kwargs, stop_on_response)
238 logger.debug('Event %s: calling handler %s', event_name, handler)
--> 239 response = handler(**kwargs)
240 responses.append((handler, response))
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/signers.py:105, in RequestSigner.handler(self, operation_name, request, **kwargs)
100 def handler(self, operation_name=None, request=None, **kwargs):
101 # This is typically hooked up to the "request-created" event
102 # from a client's event emitter. When a new request is created
103 # this method is invoked to sign the request.
104 # Don't call this method directly.
--> 105 return self.sign(operation_name, request)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/signers.py:190, in RequestSigner.sign(self, operation_name, request, region_name, signing_type, expires_in, signing_name)
189 try:
--> 190 auth = self.get_auth_instance(**kwargs)
191 except UnknownSignatureVersionError as e:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/signers.py:310, in RequestSigner.get_auth_instance(self, signing_name, region_name, signature_version, request_credentials, **kwargs)
309 if credentials is not None:
--> 310 frozen_credentials = credentials.get_frozen_credentials()
311 kwargs['credentials'] = frozen_credentials
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/credentials.py:634, in RefreshableCredentials.get_frozen_credentials(self)
601 """Return immutable credentials.
602
603 The access_key
, secret_key
, and token
properties
(...)
632
633 """
--> 634 self._refresh()
635 return self._frozen_credentials
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/credentials.py:522, in RefreshableCredentials._refresh(self)
519 is_mandatory_refresh = self.refresh_needed(
520 self._mandatory_refresh_timeout
521 )
--> 522 self._protected_refresh(is_mandatory=is_mandatory_refresh)
523 return
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/credentials.py:538, in RefreshableCredentials._protected_refresh(self, is_mandatory)
537 try:
--> 538 metadata = self._refresh_using()
539 except Exception:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/credentials.py:685, in CachedCredentialFetcher.fetch_credentials(self)
684 def fetch_credentials(self):
--> 685 return self._get_cached_credentials()
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/credentials.py:695, in CachedCredentialFetcher._get_cached_credentials(self)
694 if response is None:
--> 695 response = self._get_credentials()
696 self._write_to_cache(response)
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/credentials.py:2150, in SSOCredentialFetcher._get_credentials(self)
2149 initial_token_data = self._token_provider.load_token()
-> 2150 token = initial_token_data.get_frozen_token().token
2151 else:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/tokens.py:82, in DeferredRefreshableToken.get_frozen_token(self)
81 def get_frozen_token(self):
---> 82 self._refresh()
83 return self._frozen_token
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/tokens.py:95, in DeferredRefreshableToken._refresh(self)
94 try:
---> 95 self._protected_refresh()
96 finally:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/tokens.py:109, in DeferredRefreshableToken._protected_refresh(self)
108 self._next_refresh = now + timedelta(seconds=self._attempt_timeout)
--> 109 self._frozen_token = self._refresh_using()
110 except Exception:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/tokens.py:312, in SSOTokenProvider._refresher(self)
311 if remaining < self._REFRESH_WINDOW:
--> 312 new_token_dict = self._refresh_access_token(token_dict)
313 if new_token_dict is not None:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/tokens.py:297, in SSOTokenProvider._refresh_access_token(self, token)
296 try:
--> 297 return self._attempt_create_token(token)
298 except ClientError:
File ~/.pyenv/versions/3.10.2/envs/project-repo/lib/python3.10/site-packages/botocore/tokens.py:262, in SSOTokenProvider._attempt_create_token(self, token)
256 response = self._client.create_token(
257 grantType=self._GRANT_TYPE,
258 clientId=token["clientId"],
259 clientSecret=token["clientSecret"],
260 refreshToken=token["refreshToken"],
261 )
--> 262 expires_in = timedelta(seconds=response["expiresIn"])
263 new_token = {
264 "startUrl": self._sso_config["sso_start_url"],
265 "region": self._sso_config["sso_region"],
(...)
271 "registrationExpiresAt": token["registrationExpiresAt"],
272 }
KeyError: 'expiresIn'
SDK version used
boto3 version: 1.34.127 botocore version: 1.34.127
Environment details (OS name and version, etc.)
14.1.1 (23B81)
Hi,
Did you check if S3 bucket have object lock enabled ?
This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.