mirror of
https://github.com/versity/versitygw.git
synced 2026-09-23 08:24:17 +00:00
98 lines
2.9 KiB
Bash
98 lines
2.9 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Copyright 2024 Versity Software
|
|
# This file is licensed under the Apache License, Version 2.0
|
|
# (the "License"); you may not use this file except in compliance
|
|
# with the License. You may obtain a copy of the License at
|
|
#
|
|
# http:#www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing,
|
|
# software distributed under the License is distributed on an
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
# KIND, either express or implied. See the License for the
|
|
# specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
get_bucket_location() {
|
|
if [[ $# -ne 2 ]]; then
|
|
log 2 "get bucket location command requires command type, bucket name"
|
|
return 1
|
|
fi
|
|
get_result=0
|
|
if [[ $1 == 's3api' ]]; then
|
|
get_bucket_location_aws "$2" || get_result=$?
|
|
elif [[ $1 == 's3cmd' ]]; then
|
|
get_bucket_location_s3cmd "$2" || get_result=$?
|
|
elif [[ $1 == 'mc' ]]; then
|
|
if ! get_bucket_location_mc "$2"; then
|
|
log 2 "error getting mc bucket location"
|
|
return 1
|
|
fi
|
|
return 0
|
|
else
|
|
log 2 "command type '$1' not implemented for get_bucket_location"
|
|
return 1
|
|
fi
|
|
if [[ $get_result -ne 0 ]]; then
|
|
return 1
|
|
fi
|
|
location=$(echo "$location_json" | jq -r '.LocationConstraint')
|
|
}
|
|
|
|
get_bucket_location_aws() {
|
|
if [[ $# -ne 1 ]]; then
|
|
log 2 "get bucket location (aws) requires bucket name"
|
|
return 1
|
|
fi
|
|
location_json=$(send_command aws --no-verify-ssl s3api get-bucket-location --bucket "$1") || location_result=$?
|
|
if [[ $location_result -ne 0 ]]; then
|
|
echo "error getting bucket location: $location"
|
|
return 1
|
|
fi
|
|
bucket_location=$(echo "$location_json" | jq -r '.LocationConstraint')
|
|
return 0
|
|
}
|
|
|
|
get_bucket_location_s3cmd() {
|
|
if [[ $# -ne 1 ]]; then
|
|
echo "get bucket location (s3cmd) requires bucket name"
|
|
return 1
|
|
fi
|
|
info=$(send_command s3cmd "${S3CMD_OPTS[@]}" --no-check-certificate info "s3://$1") || results=$?
|
|
if [[ $results -ne 0 ]]; then
|
|
log 2 "error getting bucket location: $location"
|
|
return 1
|
|
fi
|
|
log 5 "s3cmd bucket location info: $info"
|
|
bucket_location=$(echo "$info" | grep -o 'Location:.*' | awk '{print $2}')
|
|
return 0
|
|
}
|
|
|
|
get_bucket_location_mc() {
|
|
if ! check_param_count_v2 "bucket name" 1 $#; then
|
|
return 1
|
|
fi
|
|
if ! info=$(send_command mc --insecure stat "$MC_ALIAS/$1" 2>&1); then
|
|
log 2 "error getting mc info: $info"
|
|
return 1
|
|
fi
|
|
# shellcheck disable=SC2034
|
|
bucket_location=$(echo "$info" | grep -o 'Location:.*' | awk '{print $2}')
|
|
return 0
|
|
}
|
|
|
|
get_bucket_location_rest() {
|
|
if ! check_param_count_v2 "bucket, callback" 2 $#; then
|
|
return 1
|
|
fi
|
|
local bucket="$1" callback="$2"
|
|
local response
|
|
|
|
if ! response=$(send_rest_go_command_callback "200" "$callback" "-bucketName" "$bucket" "-method" "GET" "-query" "location=" 2>&1); then
|
|
log 2 "error sending rest go command: $response"
|
|
return 1
|
|
fi
|
|
echo "$response"
|
|
return 0
|
|
} |