AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.
In addition to putting an item, you can also return the item's attribute values in the same operation, using the ReturnValues parameter.
When you add an item, the primary key attribute(s) are the only required attributes. Attribute values cannot be null. String and Binary type attributes must have lengths greater than zero. Set type attributes cannot be empty. Requests with empty values will be rejected with a ValidationException exception.
You can request that PutItem return either a copy of the original item (before the update) or a copy of the updated item (after the update). For more information, see the ReturnValues description below.
To prevent a new item from replacing an existing item, use a conditional expression
that contains the attribute_not_exists
function with the name of the
attribute being used as the partition key for the table. Since every record must contain
that attribute, the attribute_not_exists
function will only succeed if
no matching item exists.
For more information about using this API, see Working with Items in the Amazon DynamoDB Developer Guide.
Namespace: Amazon.DynamoDBv2.Model
Assembly: AWSSDK.dll
Version: (assembly version)
public class PutItemRequest : AmazonDynamoDBRequest IRequestEvents
The PutItemRequest type exposes the following members
Name | Description | |
---|---|---|
PutItemRequest() | Empty constructor used to set properties independently even when a simple constructor is available | |
PutItemRequest(string, Dictionary<String, AttributeValue>) | ||
PutItemRequest(string, Dictionary<String, AttributeValue>, ReturnValue) |
Name | Type | Description | |
---|---|---|---|
ConditionalOperator | Amazon.DynamoDBv2.ConditionalOperator |
Gets and sets the property ConditionalOperator. This is a legacy parameter, for backward compatibility. New applications should use ConditionExpression instead. Do not combine legacy parameters and expression parameters in a single API call; otherwise, DynamoDB will return a ValidationException exception. A logical operator to apply to the conditions in the Expected map:
If you omit ConditionalOperator, then The operation will succeed only if the entire map evaluates to true. This parameter does not support attributes of type List or Map. |
|
ConditionExpression | System.String |
Gets and sets the property ConditionExpression.
A condition that must be satisfied in order for a conditional PutItem operation to succeed. An expression can contain any of the following:
For more information on condition expressions, see Specifying Conditions in the Amazon DynamoDB Developer Guide. ConditionExpression replaces the legacy ConditionalOperator and Expected parameters. |
|
Expected | System.Collections.Generic.Dictionary<System.String, Amazon.DynamoDBv2.Model.ExpectedAttributeValue> |
Gets and sets the property Expected. This is a legacy parameter, for backward compatibility. New applications should use ConditionExpression instead. Do not combine legacy parameters and expression parameters in a single API call; otherwise, DynamoDB will return a ValidationException exception. A map of attribute/condition pairs. Expected provides a conditional block for the PutItem operation. This parameter does not support attributes of type List or Map. Each element of Expected consists of an attribute name, a comparison operator, and one or more values. DynamoDB compares the attribute with the value(s) you supplied, using the comparison operator. For each Expected element, the result of the evaluation is either true or false. If you specify more than one element in the Expected map, then by default all of the conditions must evaluate to true. In other words, the conditions are ANDed together. (You can use the ConditionalOperator parameter to OR the conditions instead. If you do this, then at least one of the conditions must evaluate to true, rather than all of them.) If the Expected map evaluates to true, then the conditional operation succeeds; otherwise, it fails. Expected contains the following:
For usage examples of AttributeValueList and ComparisonOperator, see Legacy Conditional Parameters in the Amazon DynamoDB Developer Guide. For backward compatibility with previous DynamoDB releases, the following parameters can be used instead of AttributeValueList and ComparisonOperator:
The Value and Exists parameters are incompatible with AttributeValueList and ComparisonOperator. Note that if you use both sets of parameters at once, DynamoDB will return a ValidationException exception. |
|
ExpressionAttributeNames | System.Collections.Generic.Dictionary<System.String, System.String> |
Gets and sets the property ExpressionAttributeNames.
One or more substitution tokens for attribute names in an expression. The following are some use cases for using ExpressionAttributeNames:
Use the # character in an expression to dereference an attribute name. For example, consider the following attribute name:
The name of this attribute conflicts with a reserved word, so it cannot be used directly in an expression. (For the complete list of reserved words, see Reserved Words in the Amazon DynamoDB Developer Guide). To work around this, you could specify the following for ExpressionAttributeNames:
You could then use this substitution in an expression, as in this example:
Tokens that begin with the : character are expression attribute values, which are placeholders for the actual value at runtime. For more information on expression attribute names, see Accessing Item Attributes in the Amazon DynamoDB Developer Guide. |
|
ExpressionAttributeValues | System.Collections.Generic.Dictionary<System.String, Amazon.DynamoDBv2.Model.AttributeValue> |
Gets and sets the property ExpressionAttributeValues.
One or more values that can be substituted in an expression. Use the : (colon) character in an expression to dereference an attribute value. For example, suppose that you wanted to check whether the value of the ProductStatus attribute was one of the following:
You would first need to specify ExpressionAttributeValues as follows: { ":avail":{"S":"Available"}, ":back":{"S":"Backordered"}, ":disc":{"S":"Discontinued"} } You could then use these values in an expression, such as this:
For more information on expression attribute values, see Specifying Conditions in the Amazon DynamoDB Developer Guide. |
|
Item | System.Collections.Generic.Dictionary<System.String, Amazon.DynamoDBv2.Model.AttributeValue> |
Gets and sets the property Item.
A map of attribute name/value pairs, one for each attribute. Only the primary key attributes are required; you can optionally provide other attribute name-value pairs for the item. You must provide all of the attributes for the primary key. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide both values for both the partition key and the sort key. If you specify any attributes that are part of an index key, then the data types for those attributes must match those of the schema in the table's attribute definition. For more information about primary keys, see Primary Key in the Amazon DynamoDB Developer Guide. Each element in the Item map is an AttributeValue object. |
|
ReturnConsumedCapacity | Amazon.DynamoDBv2.ReturnConsumedCapacity | Gets and sets the property ReturnConsumedCapacity. | |
ReturnItemCollectionMetrics | Amazon.DynamoDBv2.ReturnItemCollectionMetrics |
Gets and sets the property ReturnItemCollectionMetrics.
Determines whether item collection metrics are returned. If set to |
|
ReturnValues | Amazon.DynamoDBv2.ReturnValue |
Gets and sets the property ReturnValues.
Use ReturnValues if you want to get the item attributes as they appeared before they were updated with the PutItem request. For PutItem, the valid values are:
|
|
TableName | System.String |
Gets and sets the property TableName.
The name of the table to contain the item. |
This example shows how to put an item in a table.
// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item attributes Dictionary<string, AttributeValue> attributes = new Dictionary<string, AttributeValue>(); // Author is hash-key attributes["Author"] = new AttributeValue { S = "Mark Twain" }; // Title is range-key attributes["Title"] = new AttributeValue { S = "The Adventures of Tom Sawyer" }; // Other attributes attributes["Year"] = new AttributeValue { N = "1876" }; attributes["Setting"] = new AttributeValue { S = "Missouri" }; attributes["Pages"] = new AttributeValue { N = "275" }; attributes["Genres"] = new AttributeValue { SS = new List<string> { "Satire", "Folk", "Children's Novel" } }; // Create PutItem request PutItemRequest request = new PutItemRequest { TableName = "SampleTable", Item = attributes }; // Issue PutItem request client.PutItem(request);
.NET Framework:
Supported in: 4.5, 4.0, 3.5
.NET for Windows Store apps:
Supported in: Windows 8.1, Windows 8
.NET for Windows Phone:
Supported in: Windows Phone 8.1, Windows Phone 8