examples_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.
  2. package dynamodb_test
  3. import (
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/awserr"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. "github.com/aws/aws-sdk-go/service/dynamodb"
  11. )
  12. var _ time.Duration
  13. var _ strings.Reader
  14. var _ aws.Config
  15. func parseTime(layout, value string) *time.Time {
  16. t, err := time.Parse(layout, value)
  17. if err != nil {
  18. panic(err)
  19. }
  20. return &t
  21. }
  22. // To retrieve multiple items from a table
  23. //
  24. // This example reads multiple items from the Music table using a batch of three GetItem
  25. // requests. Only the AlbumTitle attribute is returned.
  26. func ExampleDynamoDB_BatchGetItem_shared00() {
  27. svc := dynamodb.New(session.New())
  28. input := &dynamodb.BatchGetItemInput{
  29. RequestItems: map[string]*dynamodb.KeysAndAttributes{
  30. "Music": {
  31. Keys: []map[string]*dynamodb.AttributeValue{
  32. {
  33. "Artist": &dynamodb.AttributeValue{
  34. S: aws.String("No One You Know"),
  35. },
  36. "SongTitle": &dynamodb.AttributeValue{
  37. S: aws.String("Call Me Today"),
  38. },
  39. },
  40. {
  41. "Artist": &dynamodb.AttributeValue{
  42. S: aws.String("Acme Band"),
  43. },
  44. "SongTitle": &dynamodb.AttributeValue{
  45. S: aws.String("Happy Day"),
  46. },
  47. },
  48. {
  49. "Artist": &dynamodb.AttributeValue{
  50. S: aws.String("No One You Know"),
  51. },
  52. "SongTitle": &dynamodb.AttributeValue{
  53. S: aws.String("Scared of My Shadow"),
  54. },
  55. },
  56. },
  57. ProjectionExpression: aws.String("AlbumTitle"),
  58. },
  59. },
  60. }
  61. result, err := svc.BatchGetItem(input)
  62. if err != nil {
  63. if aerr, ok := err.(awserr.Error); ok {
  64. switch aerr.Code() {
  65. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  66. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  67. case dynamodb.ErrCodeResourceNotFoundException:
  68. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  69. case dynamodb.ErrCodeRequestLimitExceeded:
  70. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  71. case dynamodb.ErrCodeInternalServerError:
  72. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  73. default:
  74. fmt.Println(aerr.Error())
  75. }
  76. } else {
  77. // Print the error, cast err to awserr.Error to get the Code and
  78. // Message from an error.
  79. fmt.Println(err.Error())
  80. }
  81. return
  82. }
  83. fmt.Println(result)
  84. }
  85. // To add multiple items to a table
  86. //
  87. // This example adds three new items to the Music table using a batch of three PutItem
  88. // requests.
  89. func ExampleDynamoDB_BatchWriteItem_shared00() {
  90. svc := dynamodb.New(session.New())
  91. input := &dynamodb.BatchWriteItemInput{
  92. RequestItems: map[string][]*dynamodb.WriteRequest{
  93. "Music": {
  94. {
  95. PutRequest: &dynamodb.PutRequest{
  96. Item: map[string]*dynamodb.AttributeValue{
  97. "AlbumTitle": {
  98. S: aws.String("Somewhat Famous"),
  99. },
  100. "Artist": {
  101. S: aws.String("No One You Know"),
  102. },
  103. "SongTitle": {
  104. S: aws.String("Call Me Today"),
  105. },
  106. },
  107. },
  108. },
  109. {
  110. PutRequest: &dynamodb.PutRequest{
  111. Item: map[string]*dynamodb.AttributeValue{
  112. "AlbumTitle": {
  113. S: aws.String("Songs About Life"),
  114. },
  115. "Artist": {
  116. S: aws.String("Acme Band"),
  117. },
  118. "SongTitle": {
  119. S: aws.String("Happy Day"),
  120. },
  121. },
  122. },
  123. },
  124. {
  125. PutRequest: &dynamodb.PutRequest{
  126. Item: map[string]*dynamodb.AttributeValue{
  127. "AlbumTitle": {
  128. S: aws.String("Blue Sky Blues"),
  129. },
  130. "Artist": {
  131. S: aws.String("No One You Know"),
  132. },
  133. "SongTitle": {
  134. S: aws.String("Scared of My Shadow"),
  135. },
  136. },
  137. },
  138. },
  139. },
  140. },
  141. }
  142. result, err := svc.BatchWriteItem(input)
  143. if err != nil {
  144. if aerr, ok := err.(awserr.Error); ok {
  145. switch aerr.Code() {
  146. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  147. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  148. case dynamodb.ErrCodeResourceNotFoundException:
  149. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  150. case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
  151. fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
  152. case dynamodb.ErrCodeRequestLimitExceeded:
  153. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  154. case dynamodb.ErrCodeInternalServerError:
  155. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  156. default:
  157. fmt.Println(aerr.Error())
  158. }
  159. } else {
  160. // Print the error, cast err to awserr.Error to get the Code and
  161. // Message from an error.
  162. fmt.Println(err.Error())
  163. }
  164. return
  165. }
  166. fmt.Println(result)
  167. }
  168. // To create a table
  169. //
  170. // This example creates a table named Music.
  171. func ExampleDynamoDB_CreateTable_shared00() {
  172. svc := dynamodb.New(session.New())
  173. input := &dynamodb.CreateTableInput{
  174. AttributeDefinitions: []*dynamodb.AttributeDefinition{
  175. {
  176. AttributeName: aws.String("Artist"),
  177. AttributeType: aws.String("S"),
  178. },
  179. {
  180. AttributeName: aws.String("SongTitle"),
  181. AttributeType: aws.String("S"),
  182. },
  183. },
  184. KeySchema: []*dynamodb.KeySchemaElement{
  185. {
  186. AttributeName: aws.String("Artist"),
  187. KeyType: aws.String("HASH"),
  188. },
  189. {
  190. AttributeName: aws.String("SongTitle"),
  191. KeyType: aws.String("RANGE"),
  192. },
  193. },
  194. ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
  195. ReadCapacityUnits: aws.Int64(5),
  196. WriteCapacityUnits: aws.Int64(5),
  197. },
  198. TableName: aws.String("Music"),
  199. }
  200. result, err := svc.CreateTable(input)
  201. if err != nil {
  202. if aerr, ok := err.(awserr.Error); ok {
  203. switch aerr.Code() {
  204. case dynamodb.ErrCodeResourceInUseException:
  205. fmt.Println(dynamodb.ErrCodeResourceInUseException, aerr.Error())
  206. case dynamodb.ErrCodeLimitExceededException:
  207. fmt.Println(dynamodb.ErrCodeLimitExceededException, aerr.Error())
  208. case dynamodb.ErrCodeInternalServerError:
  209. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  210. default:
  211. fmt.Println(aerr.Error())
  212. }
  213. } else {
  214. // Print the error, cast err to awserr.Error to get the Code and
  215. // Message from an error.
  216. fmt.Println(err.Error())
  217. }
  218. return
  219. }
  220. fmt.Println(result)
  221. }
  222. // To delete an item
  223. //
  224. // This example deletes an item from the Music table.
  225. func ExampleDynamoDB_DeleteItem_shared00() {
  226. svc := dynamodb.New(session.New())
  227. input := &dynamodb.DeleteItemInput{
  228. Key: map[string]*dynamodb.AttributeValue{
  229. "Artist": {
  230. S: aws.String("No One You Know"),
  231. },
  232. "SongTitle": {
  233. S: aws.String("Scared of My Shadow"),
  234. },
  235. },
  236. TableName: aws.String("Music"),
  237. }
  238. result, err := svc.DeleteItem(input)
  239. if err != nil {
  240. if aerr, ok := err.(awserr.Error); ok {
  241. switch aerr.Code() {
  242. case dynamodb.ErrCodeConditionalCheckFailedException:
  243. fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
  244. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  245. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  246. case dynamodb.ErrCodeResourceNotFoundException:
  247. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  248. case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
  249. fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
  250. case dynamodb.ErrCodeTransactionConflictException:
  251. fmt.Println(dynamodb.ErrCodeTransactionConflictException, aerr.Error())
  252. case dynamodb.ErrCodeRequestLimitExceeded:
  253. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  254. case dynamodb.ErrCodeInternalServerError:
  255. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  256. default:
  257. fmt.Println(aerr.Error())
  258. }
  259. } else {
  260. // Print the error, cast err to awserr.Error to get the Code and
  261. // Message from an error.
  262. fmt.Println(err.Error())
  263. }
  264. return
  265. }
  266. fmt.Println(result)
  267. }
  268. // To delete a table
  269. //
  270. // This example deletes the Music table.
  271. func ExampleDynamoDB_DeleteTable_shared00() {
  272. svc := dynamodb.New(session.New())
  273. input := &dynamodb.DeleteTableInput{
  274. TableName: aws.String("Music"),
  275. }
  276. result, err := svc.DeleteTable(input)
  277. if err != nil {
  278. if aerr, ok := err.(awserr.Error); ok {
  279. switch aerr.Code() {
  280. case dynamodb.ErrCodeResourceInUseException:
  281. fmt.Println(dynamodb.ErrCodeResourceInUseException, aerr.Error())
  282. case dynamodb.ErrCodeResourceNotFoundException:
  283. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  284. case dynamodb.ErrCodeLimitExceededException:
  285. fmt.Println(dynamodb.ErrCodeLimitExceededException, aerr.Error())
  286. case dynamodb.ErrCodeInternalServerError:
  287. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  288. default:
  289. fmt.Println(aerr.Error())
  290. }
  291. } else {
  292. // Print the error, cast err to awserr.Error to get the Code and
  293. // Message from an error.
  294. fmt.Println(err.Error())
  295. }
  296. return
  297. }
  298. fmt.Println(result)
  299. }
  300. // To determine capacity limits per table and account, in the current AWS region
  301. //
  302. // The following example returns the maximum read and write capacity units per table,
  303. // and for the AWS account, in the current AWS region.
  304. func ExampleDynamoDB_DescribeLimits_shared00() {
  305. svc := dynamodb.New(session.New())
  306. input := &dynamodb.DescribeLimitsInput{}
  307. result, err := svc.DescribeLimits(input)
  308. if err != nil {
  309. if aerr, ok := err.(awserr.Error); ok {
  310. switch aerr.Code() {
  311. case dynamodb.ErrCodeInternalServerError:
  312. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  313. default:
  314. fmt.Println(aerr.Error())
  315. }
  316. } else {
  317. // Print the error, cast err to awserr.Error to get the Code and
  318. // Message from an error.
  319. fmt.Println(err.Error())
  320. }
  321. return
  322. }
  323. fmt.Println(result)
  324. }
  325. // To describe a table
  326. //
  327. // This example describes the Music table.
  328. func ExampleDynamoDB_DescribeTable_shared00() {
  329. svc := dynamodb.New(session.New())
  330. input := &dynamodb.DescribeTableInput{
  331. TableName: aws.String("Music"),
  332. }
  333. result, err := svc.DescribeTable(input)
  334. if err != nil {
  335. if aerr, ok := err.(awserr.Error); ok {
  336. switch aerr.Code() {
  337. case dynamodb.ErrCodeResourceNotFoundException:
  338. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  339. case dynamodb.ErrCodeInternalServerError:
  340. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  341. default:
  342. fmt.Println(aerr.Error())
  343. }
  344. } else {
  345. // Print the error, cast err to awserr.Error to get the Code and
  346. // Message from an error.
  347. fmt.Println(err.Error())
  348. }
  349. return
  350. }
  351. fmt.Println(result)
  352. }
  353. // To read an item from a table
  354. //
  355. // This example retrieves an item from the Music table. The table has a partition key
  356. // and a sort key (Artist and SongTitle), so you must specify both of these attributes.
  357. func ExampleDynamoDB_GetItem_shared00() {
  358. svc := dynamodb.New(session.New())
  359. input := &dynamodb.GetItemInput{
  360. Key: map[string]*dynamodb.AttributeValue{
  361. "Artist": {
  362. S: aws.String("Acme Band"),
  363. },
  364. "SongTitle": {
  365. S: aws.String("Happy Day"),
  366. },
  367. },
  368. TableName: aws.String("Music"),
  369. }
  370. result, err := svc.GetItem(input)
  371. if err != nil {
  372. if aerr, ok := err.(awserr.Error); ok {
  373. switch aerr.Code() {
  374. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  375. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  376. case dynamodb.ErrCodeResourceNotFoundException:
  377. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  378. case dynamodb.ErrCodeRequestLimitExceeded:
  379. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  380. case dynamodb.ErrCodeInternalServerError:
  381. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  382. default:
  383. fmt.Println(aerr.Error())
  384. }
  385. } else {
  386. // Print the error, cast err to awserr.Error to get the Code and
  387. // Message from an error.
  388. fmt.Println(err.Error())
  389. }
  390. return
  391. }
  392. fmt.Println(result)
  393. }
  394. // To list tables
  395. //
  396. // This example lists all of the tables associated with the current AWS account and
  397. // endpoint.
  398. func ExampleDynamoDB_ListTables_shared00() {
  399. svc := dynamodb.New(session.New())
  400. input := &dynamodb.ListTablesInput{}
  401. result, err := svc.ListTables(input)
  402. if err != nil {
  403. if aerr, ok := err.(awserr.Error); ok {
  404. switch aerr.Code() {
  405. case dynamodb.ErrCodeInternalServerError:
  406. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  407. default:
  408. fmt.Println(aerr.Error())
  409. }
  410. } else {
  411. // Print the error, cast err to awserr.Error to get the Code and
  412. // Message from an error.
  413. fmt.Println(err.Error())
  414. }
  415. return
  416. }
  417. fmt.Println(result)
  418. }
  419. // To add an item to a table
  420. //
  421. // This example adds a new item to the Music table.
  422. func ExampleDynamoDB_PutItem_shared00() {
  423. svc := dynamodb.New(session.New())
  424. input := &dynamodb.PutItemInput{
  425. Item: map[string]*dynamodb.AttributeValue{
  426. "AlbumTitle": {
  427. S: aws.String("Somewhat Famous"),
  428. },
  429. "Artist": {
  430. S: aws.String("No One You Know"),
  431. },
  432. "SongTitle": {
  433. S: aws.String("Call Me Today"),
  434. },
  435. },
  436. ReturnConsumedCapacity: aws.String("TOTAL"),
  437. TableName: aws.String("Music"),
  438. }
  439. result, err := svc.PutItem(input)
  440. if err != nil {
  441. if aerr, ok := err.(awserr.Error); ok {
  442. switch aerr.Code() {
  443. case dynamodb.ErrCodeConditionalCheckFailedException:
  444. fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
  445. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  446. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  447. case dynamodb.ErrCodeResourceNotFoundException:
  448. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  449. case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
  450. fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
  451. case dynamodb.ErrCodeTransactionConflictException:
  452. fmt.Println(dynamodb.ErrCodeTransactionConflictException, aerr.Error())
  453. case dynamodb.ErrCodeRequestLimitExceeded:
  454. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  455. case dynamodb.ErrCodeInternalServerError:
  456. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  457. default:
  458. fmt.Println(aerr.Error())
  459. }
  460. } else {
  461. // Print the error, cast err to awserr.Error to get the Code and
  462. // Message from an error.
  463. fmt.Println(err.Error())
  464. }
  465. return
  466. }
  467. fmt.Println(result)
  468. }
  469. // To query an item
  470. //
  471. // This example queries items in the Music table. The table has a partition key and
  472. // sort key (Artist and SongTitle), but this query only specifies the partition key
  473. // value. It returns song titles by the artist named "No One You Know".
  474. func ExampleDynamoDB_Query_shared00() {
  475. svc := dynamodb.New(session.New())
  476. input := &dynamodb.QueryInput{
  477. ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
  478. ":v1": {
  479. S: aws.String("No One You Know"),
  480. },
  481. },
  482. KeyConditionExpression: aws.String("Artist = :v1"),
  483. ProjectionExpression: aws.String("SongTitle"),
  484. TableName: aws.String("Music"),
  485. }
  486. result, err := svc.Query(input)
  487. if err != nil {
  488. if aerr, ok := err.(awserr.Error); ok {
  489. switch aerr.Code() {
  490. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  491. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  492. case dynamodb.ErrCodeResourceNotFoundException:
  493. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  494. case dynamodb.ErrCodeRequestLimitExceeded:
  495. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  496. case dynamodb.ErrCodeInternalServerError:
  497. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  498. default:
  499. fmt.Println(aerr.Error())
  500. }
  501. } else {
  502. // Print the error, cast err to awserr.Error to get the Code and
  503. // Message from an error.
  504. fmt.Println(err.Error())
  505. }
  506. return
  507. }
  508. fmt.Println(result)
  509. }
  510. // To scan a table
  511. //
  512. // This example scans the entire Music table, and then narrows the results to songs
  513. // by the artist "No One You Know". For each item, only the album title and song title
  514. // are returned.
  515. func ExampleDynamoDB_Scan_shared00() {
  516. svc := dynamodb.New(session.New())
  517. input := &dynamodb.ScanInput{
  518. ExpressionAttributeNames: map[string]*string{
  519. "AT": aws.String("AlbumTitle"),
  520. "ST": aws.String("SongTitle"),
  521. },
  522. ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
  523. ":a": {
  524. S: aws.String("No One You Know"),
  525. },
  526. },
  527. FilterExpression: aws.String("Artist = :a"),
  528. ProjectionExpression: aws.String("#ST, #AT"),
  529. TableName: aws.String("Music"),
  530. }
  531. result, err := svc.Scan(input)
  532. if err != nil {
  533. if aerr, ok := err.(awserr.Error); ok {
  534. switch aerr.Code() {
  535. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  536. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  537. case dynamodb.ErrCodeResourceNotFoundException:
  538. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  539. case dynamodb.ErrCodeRequestLimitExceeded:
  540. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  541. case dynamodb.ErrCodeInternalServerError:
  542. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  543. default:
  544. fmt.Println(aerr.Error())
  545. }
  546. } else {
  547. // Print the error, cast err to awserr.Error to get the Code and
  548. // Message from an error.
  549. fmt.Println(err.Error())
  550. }
  551. return
  552. }
  553. fmt.Println(result)
  554. }
  555. // To update an item in a table
  556. //
  557. // This example updates an item in the Music table. It adds a new attribute (Year) and
  558. // modifies the AlbumTitle attribute. All of the attributes in the item, as they appear
  559. // after the update, are returned in the response.
  560. func ExampleDynamoDB_UpdateItem_shared00() {
  561. svc := dynamodb.New(session.New())
  562. input := &dynamodb.UpdateItemInput{
  563. ExpressionAttributeNames: map[string]*string{
  564. "#AT": aws.String("AlbumTitle"),
  565. "#Y": aws.String("Year"),
  566. },
  567. ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
  568. ":t": {
  569. S: aws.String("Louder Than Ever"),
  570. },
  571. ":y": {
  572. N: aws.String("2015"),
  573. },
  574. },
  575. Key: map[string]*dynamodb.AttributeValue{
  576. "Artist": {
  577. S: aws.String("Acme Band"),
  578. },
  579. "SongTitle": {
  580. S: aws.String("Happy Day"),
  581. },
  582. },
  583. ReturnValues: aws.String("ALL_NEW"),
  584. TableName: aws.String("Music"),
  585. UpdateExpression: aws.String("SET #Y = :y, #AT = :t"),
  586. }
  587. result, err := svc.UpdateItem(input)
  588. if err != nil {
  589. if aerr, ok := err.(awserr.Error); ok {
  590. switch aerr.Code() {
  591. case dynamodb.ErrCodeConditionalCheckFailedException:
  592. fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
  593. case dynamodb.ErrCodeProvisionedThroughputExceededException:
  594. fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
  595. case dynamodb.ErrCodeResourceNotFoundException:
  596. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  597. case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
  598. fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
  599. case dynamodb.ErrCodeTransactionConflictException:
  600. fmt.Println(dynamodb.ErrCodeTransactionConflictException, aerr.Error())
  601. case dynamodb.ErrCodeRequestLimitExceeded:
  602. fmt.Println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
  603. case dynamodb.ErrCodeInternalServerError:
  604. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  605. default:
  606. fmt.Println(aerr.Error())
  607. }
  608. } else {
  609. // Print the error, cast err to awserr.Error to get the Code and
  610. // Message from an error.
  611. fmt.Println(err.Error())
  612. }
  613. return
  614. }
  615. fmt.Println(result)
  616. }
  617. // To modify a table's provisioned throughput
  618. //
  619. // This example increases the provisioned read and write capacity on the Music table.
  620. func ExampleDynamoDB_UpdateTable_shared00() {
  621. svc := dynamodb.New(session.New())
  622. input := &dynamodb.UpdateTableInput{
  623. ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
  624. ReadCapacityUnits: aws.Int64(10),
  625. WriteCapacityUnits: aws.Int64(10),
  626. },
  627. TableName: aws.String("MusicCollection"),
  628. }
  629. result, err := svc.UpdateTable(input)
  630. if err != nil {
  631. if aerr, ok := err.(awserr.Error); ok {
  632. switch aerr.Code() {
  633. case dynamodb.ErrCodeResourceInUseException:
  634. fmt.Println(dynamodb.ErrCodeResourceInUseException, aerr.Error())
  635. case dynamodb.ErrCodeResourceNotFoundException:
  636. fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
  637. case dynamodb.ErrCodeLimitExceededException:
  638. fmt.Println(dynamodb.ErrCodeLimitExceededException, aerr.Error())
  639. case dynamodb.ErrCodeInternalServerError:
  640. fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
  641. default:
  642. fmt.Println(aerr.Error())
  643. }
  644. } else {
  645. // Print the error, cast err to awserr.Error to get the Code and
  646. // Message from an error.
  647. fmt.Println(err.Error())
  648. }
  649. return
  650. }
  651. fmt.Println(result)
  652. }