MCPcopy Create free account
hub / github.com/pyathena-dev/PyAthena / setxattr

Method setxattr

pyathena/filesystem/s3.py:1459–1507  ·  view source on GitHub ↗

Set the user-defined metadata of the path. S3 does not allow updating the metadata of an existing object in place, so the object is copied onto itself with the REPLACE metadata directive. Note that this rewrites the object and updates its last-modified time.

(self, path: str, copy_kwargs: dict[str, Any] | None = None, **kw_args)

Source from the content-addressed store, hash-verified

1457 return self.metadata(path, **kwargs).get(attr_name)
1458
1459 def setxattr(self, path: str, copy_kwargs: dict[str, Any] | None = None, **kw_args) -> None:
1460 """Set the user-defined metadata of the path.
1461
1462 S3 does not allow updating the metadata of an existing object in
1463 place, so the object is copied onto itself with the REPLACE metadata
1464 directive. Note that this rewrites the object and updates its
1465 last-modified time.
1466
1467 Args:
1468 path: S3 path (s3://bucket/key) to set metadata for.
1469 copy_kwargs: Additional parameters to use for the underlying
1470 CopyObject API call.
1471 **kw_args: Key-value pairs to set, where the values must be
1472 strings. The keys are used as-is; names that are not valid
1473 Python identifiers (e.g., containing hyphens) can be passed
1474 by unpacking a dictionary. Does not alter existing fields,
1475 unless the field appears here - if the value is None, delete
1476 the field.
1477
1478 Example:
1479 >>> fs = S3FileSystem()
1480 >>> fs.setxattr("s3://bucket/key", attribute1="value1")
1481 >>> fs.setxattr("s3://bucket/key", **{"attribute-2": "value2"})
1482 """
1483 bucket, key, version_id = self.parse_path(path)
1484 if not key:
1485 raise ValueError("Cannot set metadata of a bucket.")
1486 metadata = dict(self.metadata(path))
1487 for k, v in kw_args.items():
1488 if v is None:
1489 metadata.pop(k, None)
1490 else:
1491 metadata[k] = v
1492
1493 copy_source: dict[str, Any] = {"Bucket": bucket, "Key": key}
1494 if version_id:
1495 copy_source.update({"VersionId": version_id})
1496
1497 _logger.debug(f"Set object metadata: s3://{bucket}/{key}?versionId={version_id}")
1498 self._call(
1499 self._client.copy_object,
1500 CopySource=copy_source,
1501 Bucket=bucket,
1502 Key=key,
1503 Metadata=metadata,
1504 MetadataDirective="REPLACE",
1505 **(copy_kwargs if copy_kwargs else {}),
1506 )
1507 self.invalidate_cache(path)
1508
1509 def get_tags(self, path: str) -> dict[str, str]:
1510 """Retrieve the tag key/values for the given path.

Callers

nothing calls this directly

Calls 5

parse_pathMethod · 0.95
metadataMethod · 0.95
_callMethod · 0.95
invalidate_cacheMethod · 0.95
updateMethod · 0.45

Tested by

no test coverage detected