|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +"""This module exposes BigQuery ObjectRef functions. |
| 17 | +
|
| 18 | +See bigframes.bigquery.obj for public docs. |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import datetime |
| 25 | +from typing import Optional, Sequence, Union |
| 26 | + |
| 27 | +import numpy as np |
| 28 | +import pandas as pd |
| 29 | + |
| 30 | +from bigframes.core import convert |
| 31 | +from bigframes.core.logging import log_adapter |
| 32 | +import bigframes.core.utils as utils |
| 33 | +import bigframes.operations as ops |
| 34 | +import bigframes.series as series |
| 35 | + |
| 36 | + |
| 37 | +@log_adapter.method_logger(custom_base_name="bigquery_obj") |
| 38 | +def fetch_metadata( |
| 39 | + objectref: series.Series, |
| 40 | +) -> series.Series: |
| 41 | + """[Preview] The OBJ.FETCH_METADATA function returns Cloud Storage metadata for a partially populated ObjectRef value. |
| 42 | +
|
| 43 | + Args: |
| 44 | + objectref (bigframes.pandas.Series): |
| 45 | + A partially populated ObjectRef value, in which the uri and authorizer fields are populated and the details field isn't. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + bigframes.pandas.Series: A fully populated ObjectRef value. The metadata is provided in the details field of the returned ObjectRef value. |
| 49 | + """ |
| 50 | + objectref = convert.to_bf_series(objectref, default_index=None) |
| 51 | + return objectref._apply_unary_op(ops.obj_fetch_metadata_op) |
| 52 | + |
| 53 | + |
| 54 | +@log_adapter.method_logger(custom_base_name="bigquery_obj") |
| 55 | +def get_access_url( |
| 56 | + objectref: series.Series, |
| 57 | + mode: str, |
| 58 | + duration: Optional[Union[datetime.timedelta, pd.Timedelta, np.timedelta64]] = None, |
| 59 | +) -> series.Series: |
| 60 | + """[Preview] The OBJ.GET_ACCESS_URL function returns JSON that contains reference information for the input ObjectRef value, and also access URLs that you can use to read or modify the Cloud Storage object. |
| 61 | +
|
| 62 | + Args: |
| 63 | + objectref (bigframes.pandas.Series): |
| 64 | + An ObjectRef value that represents a Cloud Storage object. |
| 65 | + mode (str): |
| 66 | + A STRING value that identifies the type of URL that you want to be returned. The following values are supported: |
| 67 | + 'r': Returns a URL that lets you read the object. |
| 68 | + 'rw': Returns two URLs, one that lets you read the object, and one that lets you modify the object. |
| 69 | + duration (Union[datetime.timedelta, pandas.Timedelta, numpy.timedelta64], optional): |
| 70 | + An optional INTERVAL value that specifies how long the generated access URLs remain valid. You can specify a value between 30 minutes and 6 hours. For example, you could specify INTERVAL 2 HOUR to generate URLs that expire after 2 hours. The default value is 6 hours. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + bigframes.pandas.Series: A JSON value that contains the Cloud Storage object reference information from the input ObjectRef value, and also one or more URLs that you can use to access the Cloud Storage object. |
| 74 | + """ |
| 75 | + objectref = convert.to_bf_series(objectref, default_index=None) |
| 76 | + |
| 77 | + duration_micros = None |
| 78 | + if duration is not None: |
| 79 | + duration_micros = utils.timedelta_to_micros(duration) |
| 80 | + |
| 81 | + return objectref._apply_unary_op( |
| 82 | + ops.ObjGetAccessUrl(mode=mode, duration=duration_micros) |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +@log_adapter.method_logger(custom_base_name="bigquery_obj") |
| 87 | +def make_ref( |
| 88 | + uri_or_json: Union[series.Series, Sequence[str]], |
| 89 | + authorizer: Union[series.Series, str, None] = None, |
| 90 | +) -> series.Series: |
| 91 | + """[Preview] Use the OBJ.MAKE_REF function to create an ObjectRef value that contains reference information for a Cloud Storage object. |
| 92 | +
|
| 93 | + Args: |
| 94 | + uri_or_json (bigframes.pandas.Series or str): |
| 95 | + A series of STRING values that contains the URI for the Cloud Storage object, for example, gs://mybucket/flowers/12345.jpg. |
| 96 | + OR |
| 97 | + A series of JSON value that represents a Cloud Storage object. |
| 98 | + authorizer (bigframes.pandas.Series or str, optional): |
| 99 | + A STRING value that contains the Cloud Resource connection used to access the Cloud Storage object. |
| 100 | + Required if ``uri_or_json`` is a URI string. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + bigframes.pandas.Series: An ObjectRef value. |
| 104 | + """ |
| 105 | + uri_or_json = convert.to_bf_series(uri_or_json, default_index=None) |
| 106 | + |
| 107 | + if authorizer is not None: |
| 108 | + # Avoid join problems encountered if we try to convert a literal into Series. |
| 109 | + if not isinstance(authorizer, str): |
| 110 | + authorizer = convert.to_bf_series(authorizer, default_index=None) |
| 111 | + |
| 112 | + return uri_or_json._apply_binary_op(authorizer, ops.obj_make_ref_op) |
| 113 | + |
| 114 | + # If authorizer is not provided, we assume uri_or_json is a JSON objectref |
| 115 | + return uri_or_json._apply_unary_op(ops.obj_make_ref_json_op) |
0 commit comments