Lua Scripting

New in version Pacific.

Contents

This feature allows users to upload Lua scripts to different context in the radosgw. The two supported context are “preRequest” that will execute a script before the operation was taken, and “postRequest” that will execute after each operation is taken. Script may be uploaded to address requests for users of a specific tenant. The script can access fields in the request and modify some fields. All Lua language features can be used in the script.

Script Management via CLI

To upload a script:

  1. # radosgw-admin script put --infile={lua-file} --context={preRequest|postRequest} [--tenant={tenant-name}]

To print the content of the script to standard output:

  1. # radosgw-admin script get --context={preRequest|postRequest} [--tenant={tenant-name}]

To remove the script:

  1. # radosgw-admin script rm --context={preRequest|postRequest} [--tenant={tenant-name}]

Context Free Functions

Debug Log

The RGWDebugLog() function accepts a string and prints it to the debug log with priority 20. Each log message is prefixed Lua INFO:. This function has no return value.

Request Fields

Warning

This feature is experimental. Fields may be removed or renamed in the future.

Note

  • Although Lua is a case-sensitive language, field names provided by the radosgw are case-insensitive. Function names remain case-sensitive.

  • Fields marked “optional” can have a nil value.

  • Fields marked as “iterable” can be used by the pairs() function and with the # length operator.

  • All table fields can be used with the bracket operator [].

  • time fields are strings with the following format: %Y-%m-%d %H:%M:%S.

Field

Type

Description

Iterable

Writeable

Optional

Request.RGWOp

string

radosgw operation

no

no

no

Request.DecodedURI

string

decoded URI

no

no

no

Request.ContentLength

integer

size of the request

no

no

no

Request.GenericAttributes

table

string to string generic attributes map

yes

no

no

Request.Response

table

response to the request

no

no

no

Request.Response.HTTPStatusCode

integer

HTTP status code

no

yes

no

Request.Response.HTTPStatus

string

HTTP status text

no

yes

no

Request.Response.RGWCode

integer

radosgw error code

no

yes

no

Request.Response.Message

string

response message

no

yes

no

Request.SwiftAccountName

string

swift account name

no

no

yes

Request.Bucket

table

info on the bucket

no

no

yes

Request.Bucket.Tenant

string

tenant of the bucket

no

no

no

Request.Bucket.Name

string

bucket name

no

no

no

Request.Bucket.Marker

string

bucket marker (initial id)

no

no

no

Request.Bucket.Id

string

bucket id

no

no

no

Request.Bucket.Count

integer

number of objects in the bucket

no

no

no

Request.Bucket.Size

integer

total size of objects in the bucket

no

no

no

Request.Bucket.ZoneGroupId

string

zone group of the bucket

no

no

no

Request.Bucket.CreationTime

time

creation time of the bucket

no

no

no

Request.Bucket.MTime

time

modification time of the bucket

no

no

no

Request.Bucket.Quota

table

bucket quota

no

no

no

Request.Bucket.Quota.MaxSize

integer

bucket quota max size

no

no

no

Request.Bucket.Quota.MaxObjects

integer

bucket quota max number of objects

no

no

no

Reques.Bucket.Quota.Enabled

boolean

bucket quota is enabled

no

no

no

Request.Bucket.Quota.Rounded

boolean

bucket quota is rounded to 4K

no

no

no

Request.Bucket.PlacementRule

table

bucket placement rule

no

no

no

Request.Bucket.PlacementRule.Name

string

bucket placement rule name

no

no

no

Request.Bucket.PlacementRule.StorageClass

string

bucket placement rule storage class

no

no

no

Request.Bucket.User

table

bucket owner

no

no

no

Request.Bucket.User.Tenant

string

bucket owner tenant

no

no

no

Request.Bucket.User.Id

string

bucket owner id

no

no

no

Request.Object

table

info on the object

no

no

yes

Request.Object.Name

string

object name

no

no

no

Request.Object.Instance

string

object version

no

no

no

Request.Object.Id

string

object id

no

no

no

Request.Object.Size

integer

object size

no

no

no

Request.Object.MTime

time

object mtime

no

no

no

Request.CopyFrom

table

information on copy operation

no

no

yes

Request.CopyFrom.Tenant

string

tenant of the object copied from

no

no

no

Request.CopyFrom.Bucket

string

bucket of the object copied from

no

no

no

Request.CopyFrom.Object

table

object copied from. See: Request.Object

no

no

yes

Request.ObjectOwner

table

object owner

no

no

no

Request.ObjectOwner.DisplayName

string

object owner display name

no

no

no

Request.ObjectOwner.User

table

object user. See: Request.Bucket.User

no

no

no

Request.ZoneGroup.Name

string

name of zone group

no

no

no

Request.ZoneGroup.Endpoint

string

endpoint of zone group

no

no

no

Request.UserAcl

table

user ACL

no

no

no

Request.UserAcl.Owner

table

user ACL owner. See: Request.ObjectOwner

no

no

no

Request.UserAcl.Grants

table

user ACL map of string to grant note: grants without an Id are not presented when iterated and only one of them can be accessed via brackets

yes

no

no

Request.UserAcl.Grants[“<name>”]

table

user ACL grant

no

no

no

Request.UserAcl.Grants[“<name>”].Type

integer

user ACL grant type

no

no

no

Request.UserAcl.Grants[“<name>”].User

table

user ACL grant user

no

no

no

Request.UserAcl.Grants[“<name>”].User.Tenant

table

user ACL grant user tenant

no

no

no

Request.UserAcl.Grants[“<name>”].User.Id

table

user ACL grant user id

no

no

no

Request.UserAcl.Grants[“<name>”].GroupType

integer

user ACL grant group type

no

no

no

Request.UserAcl.Grants[“<name>”].Referer

string

user ACL grant referer

no

no

no

Request.BucketAcl

table

bucket ACL. See: Request.UserAcl

no

no

no

Request.ObjectAcl

table

object ACL. See: Request.UserAcl

no

no

no

Request.Environment

table

string to string environment map

yes

no

no

Request.Policy

table

policy

no

no

yes

Request.Policy.Text

string

policy text

no

no

no

Request.Policy.Id

string

policy Id

no

no

yes

Request.Policy.Statements

table

list of string statements

yes

no

no

Request.UserPolicies

table

list of user policies

yes

no

no

Request.UserPolicies[<index>]

table

user policy. See: Request.Policy

no

no

no

Request.RGWId

string

radosgw host id: <host>-<zone>-<zonegroup>

no

no

no

Request.HTTP

table

HTTP header

no

no

no

Request.HTTP.Parameters

table

string to string parameter map

yes

no

no

Request.HTTP.Resources

table

string to string resource map

yes

no

no

Request.HTTP.Metadata

table

string to string metadata map

yes

yes

no

Request.HTTP.Host

string

host name

no

no

no

Request.HTTP.Method

string

HTTP method

no

no

no

Request.HTTP.URI

string

URI

no

no

no

Request.HTTP.QueryString

string

HTTP query string

no

no

no

Request.HTTP.Domain

string

domain name

no

no

no

Request.Time

time

request time

no

no

no

Request.Dialect

string

“S3” or “Swift”

no

no

no

Request.Id

string

request Id

no

no

no

Request.TransactionId

string

transaction Id

no

no

no

Request.Tags

table

object tags map

yes

no

no

Request Functions

Operations Log

The Request.Log() function prints the requests into the operations log. This function has no parameters. It returns 0 for success and an error code if it fails.

Lua Code Samples

  • Print information on source and destination objects in case of copy:
  1. function print_object(object)
  2. RGWDebugLog(" Name: " .. object.Name)
  3. RGWDebugLog(" Instance: " .. object.Instance)
  4. RGWDebugLog(" Id: " .. object.Id)
  5. RGWDebugLog(" Size: " .. object.Size)
  6. RGWDebugLog(" MTime: " .. object.MTime)
  7. end
  8. if Request.CopyFrom and Request.Object and Request.CopyFrom.Object then
  9. RGWDebugLog("copy from object:")
  10. print_object(Request.CopyFrom.Object)
  11. RGWDebugLog("to object:")
  12. print_object(Request.Object)
  13. end
  • Print ACLs via a “generic function”:
  1. function print_owner(owner)
  2. RGWDebugLog("Owner:")
  3. RGWDebugLog(" Dispaly Name: " .. owner.DisplayName)
  4. RGWDebugLog(" Id: " .. owner.User.Id)
  5. RGWDebugLog(" Tenanet: " .. owner.User.Tenant)
  6. end
  7. function print_acl(acl_type)
  8. index = acl_type .. "ACL"
  9. acl = Request[index]
  10. if acl then
  11. RGWDebugLog(acl_type .. "ACL Owner")
  12. print_owner(acl.Owner)
  13. RGWDebugLog(" there are " .. #acl.Grants .. " grant for owner")
  14. for k,v in pairs(acl.Grants) do
  15. RGWDebugLog(" Grant Key: " .. k)
  16. RGWDebugLog(" Grant Type: " .. v.Type)
  17. RGWDebugLog(" Grant Group Type: " .. v.GroupType)
  18. RGWDebugLog(" Grant Referer: " .. v.Referer)
  19. RGWDebugLog(" Grant User Tenant: " .. v.User.Tenant)
  20. RGWDebugLog(" Grant User Id: " .. v.User.Id)
  21. end
  22. else
  23. RGWDebugLog("no " .. acl_type .. " ACL in request: " .. Request.Id)
  24. end
  25. end
  26. print_acl("User")
  27. print_acl("Bucket")
  28. print_acl("Object")
  • Use of operations log only in case of errors:
  1. if Request.Response.HTTPStatusCode ~= 200 then
  2. RGWDebugLog("request is bad, use ops log")
  3. rc = Request.Log()
  4. RGWDebugLog("ops log return code: " .. rc)
  5. end
  • Set values into the error message:
  1. if Request.Response.HTTPStatusCode == 500 then
  2. Request.Response.Message = "<Message> something bad happened :-( </Message>"
  3. end
  • Add metadata to objects that was not originally sent by the client:

In the preRequest context we should add:

  1. if Request.RGWOp == 'put_obj' then
  2. Request.HTTP.Metadata["x-amz-meta-mydata"] = "my value"
  3. end

In the postRequest context we look at the metadata:

  1. RGWDebugLog("number of metadata entries is: " .. #Request.HTTP.Metadata)
  2. for k, v in pairs(Request.HTTP.Metadata) do
  3. RGWDebugLog("key=" .. k .. ", " .. "value=" .. v)
  4. end